Is a binary JSON javascript library available for browsers?

后端 未结 3 1485
梦谈多话
梦谈多话 2021-02-01 19:14

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 me

相关标签:
3条回答
  • 2021-02-01 19:44

    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.

    0 讨论(0)
  • 2021-02-01 19:50

    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

    0 讨论(0)
  • 2021-02-01 19:52

    Here in 2019 modern browsers already have embedded the functions:

    btoa("string to be coded on base64 format") 
    atob("base64 string to be uncoded")

    As you can see here: https://caniuse.com/#search=btoa All browsers listed have this feature.

    0 讨论(0)
提交回复
热议问题