22

In order for efficient server side parsing I am looking into a BSON solution directly for the browser javascript environment. The idea is to utilize the entire ASCII space by means of binary websockets. Any suggestions?

(Any nodejs suggestions are welcome as well)

See also: http://bsonspec.org/

Lorenz Lo Sauer
  • 23,698
  • 16
  • 85
  • 87
  • 2
    I'm not sure why you would want to do this? Just pass JSON to/from the client/browser and then convert to BSON as you would like on the server. – Chris Pietschmann Sep 24 '11 at 20:04
  • JavaScript really doesn't have much support for low-level manipulations - like the ones that allow BSON to be more efficient than JSON - so you'd probably have to use Flash... – Matt Ball Sep 24 '11 at 20:11
  • 2
    @ChrisPietschmann: this would limit me to HTTP and e.g. integers in JSON have to parsed instead of the possibility of direct assignment. (The intend is also less load on the server) – Lorenz Lo Sauer Sep 24 '11 at 20:32

2 Answers2

15

For what it's worth, it appears that the MongoDB team now have a supported Javascript BSON project:

https://github.com/mongodb/js-bson

I'm no expert with the library, but the project claims to work in both Node and the browser. Below is a modified sample from their site:

<head>
  <!-- Originally https://raw.github.com/mongodb/js-bson/master/browser_build/bson.js -->
  <!-- But downloaded and hosted locally -->
  <script src="./bson.js"></script>
</head>
<body onload="start();">
<script>
  function start() {
    var BSON = bson().BSON;
    var Long = bson().Long;

    var doc = {
      oid: bson().ObjectID(),
      long: Long.fromNumber(100),
      date: new Date(),
      string: "js-bson sample",
      obj: { 
        string: "Object within an object"
      }
    }
    console.log("doc %o", doc);

    // Serialize a document
    var data = BSON.serialize(doc, false, true, false);
    console.log("data %o", data);

    // De serialize it again
    var doc_2 = BSON.deserialize(data);
    console.log("doc_2 %o", doc_2);
  }
</script>
</body>

Below are my results in Chrome:

enter image description here

Samuel Neff
  • 73,278
  • 17
  • 138
  • 182
jriggins
  • 668
  • 9
  • 15
  • 3
    Yes, it is supported in the browser, there is a browser build provided: https://github.com/mongodb/js-bson/tree/master/browser_build Also installable via bower using "bower install bson --save" – arcseldon Oct 21 '14 at 04:39
  • @SamuelNeff - That's simply not correct. Not only is it officially supported, but I've seen it used in more than a couple projects. I myself have used it as well. – JSON Nov 23 '14 at 00:20
  • @JSON, When I posted that almost two years ago it was a quote from the site itself. It said "A JS/C++ Bson parser for node, used in the MongoDB Native driver". Since then they've changed and specifically state that they support the browser. – Samuel Neff Nov 23 '14 at 04:16
  • It looks like they changed the way the import works a little bit. I had to do the following to get it to work: var bson = new BSON(); bson.serialize(...); – bruceceng Jan 17 '18 at 17:27
6

This might be incomplete but the goal of the project line up with what you want: https://github.com/muhmi/javascript-bson It doesn't look like that encodes directly to typed arrays which would be the most useful for sending over WebSocket.

kanaka
  • 70,845
  • 23
  • 144
  • 140