JSON 的二进制编码?

发布于 2024-10-19 21:36:46 字数 224 浏览 1 评论 0原文

我的 Javascript 应用程序正在从服务器下载相当多的数据,我想除了服务器完成的正常 gzip 之外,我还可以以某种二进制格式而不是文本 JSON 对数据进行编码。
有这样做的标准方法吗?
理想情况下,它应该是一些小工具,可以获取 JSON 文本文件并将其转换为通用二进制格式,以及一个对其进行解码的小型 Javascript 库。

另外,在 XHR 中是否需要做一些特殊的事情来传递二进制数据?

My Javascript application is downloading quite a bit of data from the server and I was thinking that in addition to normal gzip that's done by the server, I can encode the data in some binary format rather than textual JSON.
Is there a standard way of doing this?
Ideally it should be some small tool that can take a JSON text file and convert it to a generic binary format and a small Javascript library which decodes it.

Also, is there something special that needs to be done in XHR to pass binary data?

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

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

发布评论

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

评论(3

满地尘埃落定 2024-10-26 21:36:46

如果 gzip 压缩得不够好,那么您的二进制格式也可能无法压缩,特别是如果您不希望能够在合理的时间内通过 javascript 对其进行解码。

请记住,使用 gzip 时的解压缩是由浏览器本地完成的,并且比您在 javascript 中执行的任何操作都要快几个数量级。

如果您觉得 JSON 反序列化太慢,因为您支持 ie7 等较旧的浏览器,该浏览器不会本机解码 JSON,而是依赖于 eval 来完成工作,请考虑从 JSON 转向自定义编码基于字符串分割,反序列化速度要快得多。

如需灵感,请尝试阅读这篇文章:

http: //code.flickr.com/blog/2009/03/18/building-fast-client-side-searches/

If gzip doesn't compress well enough, chances are your binary format won't either, especially if you wan't to be able to decode it via javascript within a reasonable amount of time.

Remember that the unzipping when using gzip is done natively by the browser and is orders of magnitude faster than anything you can do in javascript.

If you feel that the JSON deserialization is too slow, because you are supporting older browsers like ie7 which doesn't decode JSON natively but depends on eval for the job, consider going away from JSON to a custom encoding based on string splitting, which is much much faster to deserialize.

For inspiration try to read this article:

http://code.flickr.com/blog/2009/03/18/building-fast-client-side-searches/

深海不蓝 2024-10-26 21:36:46

查看 BSON

BSON,二进制的缩写
JSON,是二进制编码的
类似 JSON 的序列化
文件。与 JSON 一样,BSON 支持
文档的嵌入和
其他文档中的数组以及
数组。 BSON还包含
允许的扩展
数据类型的表示
不是 JSON 规范的一部分。为了
例如,BSON 有一个 Date 类型和一个
BinData 类型。

在这里找到一个很好的解释 http://kaijaeger.com/articles/introducing -bison-binary-interchange-standard.html

Check out BSON

BSON, short for Bin­ary
JSON, is a bin­ary-en­coded
seri­al­iz­a­tion of JSON-like
doc­u­ments. Like JSON, BSON sup­ports
the em­bed­ding of doc­u­ments and
ar­rays with­in oth­er doc­u­ments and
ar­rays. BSON also con­tains
ex­ten­sions that al­low
rep­res­ent­a­tion of data types that
are not part of the JSON spec. For
ex­ample, BSON has a Date type and a
BinData type.

Find a good explanation here http://kaijaeger.com/articles/introducing-bison-binary-interchange-standard.html

束缚m 2024-10-26 21:36:46

MongoDB 正在使用类似的东西来进行面向文档的存储。您可以直接在 BSON 网站上获取更多详细信息。
不幸的是,BSON 不能与 Javascript 一起工作(正如您可以从实现列表),所以我认为这不是你问题的一个很好的答案。

您可以考虑使用协议缓冲区;它有一个 JS 编码器/解码器,但它仍然处于实验阶段。
你可以尝试一下——很多时候,实验性的开源项目已经足以在特定场景中使用。

另请注意,存在一些关于 BSON 比 JSON 更紧凑的质疑;对于其他协议(例如 protbuf)也是如此 - 我强烈建议您做一些数学计算并检查是否有实际收益。

MongoDB is using something like that for their document-oriented storage. You can get more details directly on BSON website.
Unfortunately, BSON does not work with Javascript (as you can see from the implementation list), so I think it is not a good answer to your question.

You could think about using Protocol Buffers; it has a JS encoder/decoder, but it is still quite experimental.
You may try it - lots of times, experimental open source project are already good enough for usage in specific scenarios.

Note also that there is some questioning about BSON being more compact than JSON; the same could be true also for other protocols like protbuf - I would strongly suggest you doing some math and check it out if there are actual gains.

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