7

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?

Stephan
  • 1,279
  • 8
  • 16
  • Being curious, are you sure `E` in `EJSON` is short for `Enhanced`? – Peppe L-G May 23 '14 at 08:44
  • Nope, not sure. Probably it stands for Extended JSON rather than Enhanced, but the official docs never resolve the acronym... Since I am still missing some authoritative statement on the exact meaning of `E` I will leave the question like this for now. – Stephan Apr 22 '15 at 10:08
  • I couldn't find the answer neither, so I hoped you could settle it for me. Well, one year of waiting to a waste :'( But thanks! – Peppe L-G Apr 22 '15 at 13:05

1 Answers1

10

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.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
  • So the reason we need EJSON is because browsers only speak JSON, but they do not understand BSON? – Stephan May 20 '14 at 10:57
  • 2
    @Stephan Essentially yes. BSON is a binary representation of JSON and Browsers speak JavaScript ( by consensus ) So when I am talking about "type fidelity" the concept here is something like: `{ "_id": { "$oid": "537aa577e90c3db84958aa8b" } }` then there is something there to determine the type as it were. Java and C# for example have different interpretations. JavaScript ( which is not strictly typed ) has a different interpretation. So you "Coax" it. Or give something to pass to a server that knows more. Clear know? – Neil Lunn May 20 '14 at 11:25
  • thanks, now it is clear. It gets a bit complicated when trying to make sense out of it all in a simple and clear fashion, but your answer certainly shed a lot of light :-) – Stephan May 20 '14 at 11:43