BSON is short for Binary JSON and is the binary-encoded serialization of JSON-like documents. You can learn more about it in [the specification](http://bsonspec.org).
This browser version of the BSON parser is compiled using [webpack](https://webpack.js.org/) and the current version is pre-compiled in the `browser_build` directory.
This is the default BSON parser, however, there is a C++ Node.js addon version as well that does not support the browser. It can be found at [mongod-js/bson-ext](https://github.com/mongodb-js/bson-ext).
## Usage
To build a new version perform the following operations:
The BSON `serializeWithBufferAndIndex` method takes an object, a target buffer instance and an optional options object and returns the end serialization index in the final buffer.
*@param {Object} [options.evalFunctions=false] evaluate functions in the BSON document scoped to the object deserialized.
*@param {Object} [options.cacheFunctions=false] cache evaluated functions for reuse.
*@param {Object} [options.cacheFunctionsCrc32=false] use a crc32 code for caching, otherwise use the string of the function.
*@param {Object} [options.promoteLongs=true] when deserializing a Long will fit it into a Number if it's smaller than 53 bits
*@param {Object} [options.promoteBuffers=false] when deserializing a Binary will return it as a Node.js Buffer instance.
*@param {Object} [options.promoteValues=false] when deserializing will promote BSON values to their Node.js closest equivalent types.
*@param {Object} [options.fieldsAsRaw=null] allow to specify if there what fields we wish to return as unserialized raw buffer.
*@param {Object} [options.bsonRegExp=false] return BSON regular expressions as BSONRegExp instances.
*@return {Object} returns the deserialized Javascript Object.
#### BSON.deserializeStream
The BSON `deserializeStream` method takes a Node.js Buffer, `startIndex` and allow more control over deserialization of a Buffer containing concatenated BSON documents.
*@param {Buffer} buffer the buffer containing the serialized set of BSON documents.
*@param {Number} startIndex the start index in the data Buffer where the deserialization is to start.
*@param {Number} numberOfDocuments number of documents to deserialize.
*@param {Array} documents an array where to store the deserialized documents.
*@param {Number} docStartIndex the index in the documents array from where to start inserting documents.
*@param {Object} [options.evalFunctions=false] evaluate functions in the BSON document scoped to the object deserialized.
*@param {Object} [options.cacheFunctions=false] cache evaluated functions for reuse.
*@param {Object} [options.cacheFunctionsCrc32=false] use a crc32 code for caching, otherwise use the string of the function.
*@param {Object} [options.promoteLongs=true] when deserializing a Long will fit it into a Number if it's smaller than 53 bits
*@param {Object} [options.promoteBuffers=false] when deserializing a Binary will return it as a Node.js Buffer instance.
*@param {Object} [options.promoteValues=false] when deserializing will promote BSON values to their Node.js closest equivalent types.
*@param {Object} [options.fieldsAsRaw=null] allow to specify if there what fields we wish to return as unserialized raw buffer.
*@param {Object} [options.bsonRegExp=false] return BSON regular expressions as BSONRegExp instances.
*@return {Number} returns the next index in the buffer after deserialization **x** numbers of documents.
## FAQ
#### Why does `undefined` get converted to `null`?
The `undefined` BSON type has been [deprecated for many years](http://bsonspec.org/spec.html), so this library has dropped support for it. Use the `ignoreUndefined` option (for example, from the [driver](http://mongodb.github.io/node-mongodb-native/2.2/api/MongoClient.html#connect) ) to instead remove `undefined` keys.
#### How do I add custom serialization logic?
This library looks for `toBSON()` functions on every path, and calls the `toBSON()` function to get the value to serialize.