BSON
Overview
BSON, which stands for Binary JSON, is a binary-encoded serialization of JSON-like documents. It is designed to provide a more compact storage format compared to JSON, making it particularly useful for applications where storage efficiency is crucial. BSON is self-describing, meaning it includes metadata about the data types of the fields it contains. This feature is particularly beneficial when dealing with complex data structures. BSON is widely used by MongoDB as its data storage and network transfer format. While BSON serialization is slightly slower than JSON, it offers a tradeoff between size and performance, making it a popular choice in scenarios where these factors are critical.
Converting JSON to BSON
To convert JSON data to BSON, you can use the bson library in Node.js. The following example demonstrates how to perform this conversion:
Listing 4.5 Converting JSON data to BSON (listing-4.5.js)
const fs = require('fs');
const moment = require('moment');
const BSON = require('bson');
const records = JSON.parse( #1
fs.readFileSync("./data/earthquakes.json", "utf8")
);
for (let i = 0; i < records.length; ++i) { #2
const record = records[i]; #2
record.Time = moment(record.Time).toDate(); #2
} #2
const bson = new BSON(); #3
const serializedData = bson.serialize(records); #4
fs.writeFileSync("./output/earthquakes.bson", serializedData); #5
#1 Loads earthquakes.json that we’re going to convert to BSON
#2 For each record, parse the Time value from a string to a Date object. Unlike JSON, BSON can store actual Date objects.
#3 Instances a BSON object
#4 Serializes our data to a Node.js Buffer object
#5 Writes the buffer to the binary file earthquakes.bson
In this example, we first load the JSON data from a file and parse it into JavaScript objects. We then convert any date strings into actual Date objects, which BSON can store natively. After instantiating a BSON object, we serialize the data into a binary format and write it to a .bson file.
Deserializing BSON Files
To convert BSON data back to a JavaScript object, you can deserialize it using the bson library. The following example illustrates this process:
Listing 4.6 Deserializing BSON data (listing-4.6.js)
const fs = require('fs');
const BSON = require('bson');
const loadedData = fs.readFileSync("./data/earthquakes.bson"); #1
const bson = new BSON(); #2
const deserializedData = bson.deserialize(loadedData); #3
console.log(deserializedData); #4
#1 Loads earthquakes.bson to a Node.js Buffer object
#2 Instances a BSON object
#3 Deserializes the data
#4 Prints the deserialized data so we can check that it was loaded correctly
In this example, we load the BSON data from a file into a Node.js Buffer object. We then instantiate a BSON object and use its deserialize function to decode the data back into a JavaScript object. Finally, we print the deserialized data to verify its correctness.
Installation
To use the bson library in your Node.js project, you can install it using npm:
npm install --save bson
This command will add the bson library to your project's dependencies, allowing you to perform both serialization and deserialization of BSON data as demonstrated in the examples above.
FAQ (Frequently asked questions)
What is BSON?
What are the advantages of using BSON over JSON?
Is BSON faster than JSON?
How do you convert JSON data to BSON?
What is the purpose of the BSON library in JavaScript?
Can BSON store Date objects?
What does Listing 4.5 demonstrate?
How do you deserialize a BSON file back to JavaScript?