Ultrafast BSON serializer/parser
npm install bsonfy




bsonfy is an ultrafast serializer and deserializer for the BSON format.
It is written in clean typescript and has no other lib dependencies.
BSON is mainly used as compact transport format for (JSON) objects.
Uint8Array) instead of nodejs buffers, to get this baby portable and running in browsers, too.``typescript
import { BSON } from 'bsonfy';
// create a test document
let doc = { id: 10, time: new BSON.UTC(), arr: new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]) };
// serialize the document
let bson = BSON.serialize(doc);
// and deserialize it, using BSON.UTC objects as time representation
let orig = BSON.deserialize(bson, true);
`
Basically the API consists of just two static methods to serialize/deserialize objects to/from BSON format:
BSON.serialize(object)
* @param {Object} object The Javascript object to serialize
* @return {Uint8Array} returns an Uint8Array in BSON format.
Unknown objects are ignored in serialization.
BSON.deserialize(buffer, useUTC)
* @param {Uint8Array} buffer An Uint8Array containing the BSON data
* @param {Boolean} useUTC Optional, if set a BSON.UTC object is created for 'UTC datetime' instead of a normal JS Date object. Defaults to falseundefined
* @return {Object} returns the deserialized Javascript object or in case of a parsing error (unsupported BSON element etc.)
bson.ObjectId.isValid(id) - Returns true if id is a valid number or hexadecimal string representing an ObjectId.bson.ObjectId.createFromHexString(hexString) - Returns the ObjectId the hexString represents.bson.ObjectId.createFromTime(time) - Returns an ObjectId containing the passed time.time
* - A Unix timestamp (number of seconds since the epoch).
bson.ObjectId.isValid(id) - Returns true if id is a valid number or hexadecimal string representing an ObjectId.bson.ObjectId.createFromHexString(hexString) - Returns the ObjectId the hexString represents.bson.ObjectId.createFromTime(time) - Returns an ObjectId containing the passed time.time
* - A Unix timestamp (number of seconds since the epoch).
bson.ObjectId.isValid(id) - Returns true if id is a valid number or hexadecimal string representing an ObjectId.bson.ObjectId.createFromHexString(hexString) - Returns the ObjectId the hexString represents.bson.ObjectId.createFromTime(time) - Returns an ObjectId containing the passed time.time
* - A Unix timestamp (number of seconds since the epoch).
.
Contributing
If you find any bugs, have any comments, improvements or suggestions:1. Create an issue and describe your idea
2. Fork it
3. Create your feature branch (
git checkout -b my-new-feature)
4. Commit your changes (git commit -am 'Add some feature')
5. Publish the branch (git push origin my-new-feature`)