What is bson?
The bson npm package is a library that allows you to serialize and deserialize data in BSON format. BSON, short for Binary JSON, is a binary-encoded serialization of JSON-like documents. It is designed to be efficient in both storage space and scan-speed. The bson package is commonly used when working with MongoDB, as MongoDB uses BSON as its document storage format.
What are bson's main functionalities?
Serialization
This feature allows you to convert a JavaScript object into a BSON formatted buffer. This is useful for storing and transmitting data in a compact binary form.
{"const BSON = require('bson'); const bson = new BSON(); const doc = { hello: 'world' }; const data = bson.serialize(doc); console.log(data); // <Buffer 16 00 00 00 02 68 65 6c 6c 6f 00 06 00 00 00 77 6f 72 6c 64 00 00>"}
Deserialization
This feature allows you to convert BSON data back into a JavaScript object. This is useful when you need to read data that was stored or transmitted in BSON format.
{"const BSON = require('bson'); const bson = new BSON(); const data = Buffer.from('160000000268656c6c6f0006000000776f726c640000', 'hex'); const doc = bson.deserialize(data); console.log(doc); // { hello: 'world' }"}
Other packages similar to bson
msgpack5
msgpack5 is a package that implements the MessagePack serialization format. MessagePack is an efficient binary serialization format that is similar to BSON but is not tied to MongoDB. It aims to be more compact and faster than JSON.
protobufjs
protobufjs is a package that allows you to serialize and deserialize data using Google's Protocol Buffers. Protocol Buffers are similar to BSON in that they provide a way to encode structured data in an efficient binary format, but they require a predefined schema and are more focused on cross-language compatibility.
cbor
cbor is a package that implements the CBOR (Concise Binary Object Representation) data format. Like BSON, CBOR is a binary format that can serialize and deserialize JavaScript objects. However, CBOR is designed to be more compact and to have a wider range of data types than BSON.
Javascript + C++ BSON parser
This BSON parser is primarily meant to be used with the mongodb
node.js driver.
However, wonderful tools such as onejs
can package up a BSON parser that will work in the browser.
The current build is located in the browser_build/bson.js
file.
A simple example of how to use BSON in the browser:
<html>
<head>
<script src="https://raw.github.com/mongodb/js-bson/master/browser_build/bson.js">
</script>
</head>
<body onload="start();">
<script>
function start() {
var BSON = bson().BSON;
var Long = bson().Long;
var doc = {long: Long.fromNumber(100)}
var data = BSON.serialize(doc, false, true, false);
var doc_2 = BSON.deserialize(data);
}
</script>
</body>
</html>
A simple example of how to use BSON in node.js
:
var bson = require("bson");
var BSON = bson.BSONPure.BSON;
var Long = bson.BSONPure.Long;
var doc = {long: Long.fromNumber(100)}
var data = BSON.serialize(doc, false, true, false);
console.log("data:", data);
var doc_2 = BSON.deserialize(data);
console.log("doc_2:", doc_2);
The API consists of two simple methods to serialize/deserialize objects to/from BSON format:
- BSON.serialize(object, checkKeys, asBuffer, serializeFunctions)
- @param {Object} object the Javascript object to serialize.
- @param {Boolean} checkKeys the serializer will check if keys are valid.
- @param {Boolean} asBuffer return the serialized object as a Buffer object (ignore).
- @param {Boolean} serializeFunctions serialize the javascript functions (default:false)
- @return {TypedArray/Array} returns a TypedArray or Array depending on what your browser supports
- BSON.deserialize(buffer, options, isArray)
- Options
- evalFunctions {Boolean, default:false}, evaluate functions in the BSON document scoped to the object deserialized.
- cacheFunctions {Boolean, default:false}, cache evaluated functions for reuse.
- cacheFunctionsCrc32 {Boolean, default:false}, use a crc32 code for caching, otherwise use the string of the function.
- @param {TypedArray/Array} a TypedArray/Array containing the BSON data
- @param {Object} [options] additional options used for the deserialization.
- @param {Boolean} [isArray] ignore used for recursive parsing.
- @return {Object} returns the deserialized Javascript Object.