Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Meteor use EJSON and not BSON directly?

As I understand it, Node.js supports BSON (not sure if natively or with an npm package). Meteor however invented a new flavor EJSON (Enhanced JSON), but I fail to see what advantages this brings and how it is better than using BSON directly.

Does anyone know what the advantages of EJSON over BSON are, or what reasons exist why EJSON is required when there are already JSON and BSON available?

like image 525
Stephan Avatar asked May 20 '14 08:05

Stephan


People also ask

Why is BSON faster than JSON?

BSON is also designed in a way that it has a comparatively faster encoding and decoding technique. For example, all those integers stored as 32-bit integers so that they are not parsed with them to and from the text. Therefore, it uses more space than JSON for smaller integers, but BSON is anyway much faster to parse.

Why is BSON faster?

BSON's binary structure encodes type and length information, which allows it to be traversed much more quickly compared to JSON.


1 Answers

Well it is not as if BSON has gone away, it is still actually there. The Meteor MongoDB driver section is built on top of the native node driver for MongoDB and that of course uses BSON to actually talk to MongoDB, and there is no other way since that is the language that MongoDB speaks, so to say.

AFAIK, the point of EJSON is to maintain the same sort of "type fidelity" that is inherent in BSON by it's binary definition when translating to clients that only understand JavaScript, and therefore JSON. So primarily browsers.

So as part of Meteor's goal is to make the difference between client and server side code somewhat transparent, it needs a mechanism to maintain this "type fidelity", for Dates, ObjectId etc, when transferring data to and from client and server.

So the difference of EJSON and JSON being, the JSON produced includes special keys that identifies these "types" so they can be properly processed that way, especially when talking to the server process.

like image 56
Neil Lunn Avatar answered Sep 21 '22 23:09

Neil Lunn