Google 协议缓冲区:JavaScript 示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6912981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google Protocol Buffers: JavaScript Example
提问by nikolakoco
Is it a good choice to use Google Protocol Buffers in JavaScript or is it better to do it with JSON?
在 JavaScript 中使用 Google Protocol Buffers 是一个不错的选择,还是使用 JSON 更好?
Also it'd be great if someone can give me a simple example about Protocol Buffers implementation in JavaScript. The documentation on the Google's site is scarce.
另外,如果有人能给我一个关于 JavaScript 中协议缓冲区实现的简单示例,那就太好了。Google 网站上的文档很少。
采纳答案by dmeister
[edit] Google's open source implementation of protocol buffers is available on github
[编辑] Google 的协议缓冲区的开源实现可在github 上找到
The official protobuf project support only Java, C++, and Python. Not Javascript.
官方 protobuf 项目仅支持 Java、C++ 和 Python。不是 JavaScript。
According to the Wiki of the project, there are three projects porting protocol buffers to Javascript.
根据该项目的 Wiki,有三个项目将协议缓冲区移植到 Javascript。
- Protobuf.js: https://github.com/dcodeIO/ProtoBuf.js
- protobuf-js: http://code.google.com/p/protobuf-js/
- protojs: http://github.com/sirikata/protojs
- Protobuf.js:https: //github.com/dcodeIO/ProtoBuf.js
- protobuf-js:http: //code.google.com/p/protobuf-js/
- protojs:http://github.com/sirikata/protojs
Protobuf.js is up-to-date. protobuf-js has not been updated for two years, so I would favor Protobuf.js.
Protobuf.js 是最新的。protobuf-js 已经两年没有更新了,所以我更喜欢 Protobuf.js。
The question still is "Why"?: protobuf may be a bit smaller, especially when a lot of numeric values are transferred, but JSON is simply the more common protocol in the JS space and probably better supported and easier to integrate into other tools.
问题仍然是“为什么”?:protobuf 可能更小一些,尤其是在传输大量数值时,但 JSON 只是 JS 空间中更常见的协议,并且可能得到更好的支持并且更容易集成到其他工具中。
回答by AlikElzin-kilaka
Update(28/7/2016): Release 3.0.0 was published- supporting Javascript and other languages as well - in addition to some other features.
更新(28/7/2016): 发布 3.0.0 版- 除了一些其他功能外,还支持 Javascript 和其他语言。
Google have recently added alpha support for JS to protobufs: https://github.com/google/protobuf/releases/tag/v3.0.0-beta-2
谷歌最近向 protobufs 添加了对 JS 的 alpha 支持:https: //github.com/google/protobuf/releases/tag/v3.0.0-beta-2
Usage:
用法:
protoc -I=$SRC_DIR --js_out=$DST_DIR $SRC_DIR/addressbook.proto
Screenshots from the release documentation:
发布文档的截图:
回答by Ondrej Burkert
Protobuf 3.0.0 is out: https://github.com/google/protobuf/releases/tag/v3.0.0
Protobuf 3.0.0 已发布:https: //github.com/google/protobuf/releases/tag/v3.0.0
And it support JavaScript natively. The basic information is in the announcement.
并且它本机支持 JavaScript。基本信息以公告为准。
We are going to look into it soon.
我们很快就会研究它。
回答by JeffSpicoli
Try Protostuff!
试试Protostuff吧!
I had a bit of hard time configuring but I'm sure that was more of my issue. You can serialize/deserialize a protobuff/protostuff message to/from JSON. I'm at the early stages of using this but it looks promising so far.
我在配置时遇到了一些困难,但我确信那更多是我的问题。您可以将 protobuff/protostuff 消息序列化/反序列化到/从 JSON。我正处于使用它的早期阶段,但到目前为止它看起来很有希望。