JSON 的二进制编码?
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果 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/
查看 BSON
在这里找到一个很好的解释 http://kaijaeger.com/articles/introducing -bison-binary-interchange-standard.html
Check out BSON
Find a good explanation here http://kaijaeger.com/articles/introducing-bison-binary-interchange-standard.html
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.