浏览器可以使用二进制 JSON javascript 库吗?

发布于 2024-12-06 17:34:09 字数 214 浏览 1 评论 0原文

为了高效的服务器端解析,我正在寻找直接用于浏览器 JavaScript 环境的 BSON 解决方案。这个想法是通过二进制 websocket 来利用整个 ASCII 空间。有什么建议吗?

(也欢迎任何 Nodejs 建议)

另请参阅: http://bsonspec.org/

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/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星軌x 2024-12-13 17:34:09

无论如何,MongoDB 团队现在似乎有一个受支持的 Javascript BSON 项目:

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

我不是这个库的专家,但该项目声称可以在 Node 和浏览器中工作。下面是他们网站上的修改示例:

<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>

下面是我在 Chrome 中的结果:

在此处输入图像描述

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

被你宠の有点坏 2024-12-13 17:34:09

这可能不完整,但该项目的目标与您想要的一致: https://github.com/ muhmi/javascript-bson 它看起来不像直接编码为类型化数组,而这对于通过 WebSocket 发送最有用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文