Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a binary JSON javascript library available for browsers?

Tags:

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/

like image 221
Lorenz Lo Sauer Avatar asked Sep 24 '11 20:09

Lorenz Lo Sauer


People also ask

What is BSON in JavaScript?

BSON stands for Binary Javascript Object Notation. It is a binary-encoded serialization of JSON documents. BSON has been extended to add some optional non-JSON-native data types, like dates and binary data. BSON can be compared to other binary formats, like Protocol Buffers.

What is difference between JSON and BSON?

JSON stands for JavaScript Object Notation. BSON stands for Binary JavaScript Object Notation. JSON data contains its data basic in JSON format. BSON gives extra datatypes over the JSON data.

Why does MongoDB use BSON?

Unlike systems that store JSON as string-encoded values, or binary-encoded blobs, MongoDB uses BSON to offer powerful indexing and querying features on top of the web's most popular data format.


2 Answers

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

like image 183
jriggins Avatar answered Sep 20 '22 06:09

jriggins


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.

like image 43
kanaka Avatar answered Sep 20 '22 06:09

kanaka