C++ Protobuf 到/从 JSON 转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7007876/
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
C++ Protobuf to/from JSON conversion
提问by Zaur Nasibov
Is anyone familiar with a stable C++ solution (library, code snippet etc.) which converts protobuf messages to/from JSON?
是否有人熟悉将 protobuf 消息转换为 JSON 的稳定 C++ 解决方案(库、代码片段等)?
回答by haberlerm
This one is better IMO: https://github.com/shramov/json2pb
这是更好的 IMO:https: //github.com/shramov/json2pb
it does conversion in both directions and handles extensions
它在两个方向进行转换并处理扩展
回答by yinqiwen
回答by Andrés Senac
I've made a bootstrap implementation of a JSON parser for protobuf generated types, using the its reflection mechanism, and adapting the parse I've made previously for CORBA IDL generated types.
我已经为 protobuf 生成的类型制作了一个 JSON 解析器的引导实现,使用它的反射机制,并调整了我之前为 CORBA IDL 生成的类型所做的解析。
You can find it at http://corbasim.googlecode.com/svn/trunk/protobuf2json_exported.zip
您可以在http://corbasim.googlecode.com/svn/trunk/protobuf2json_exported.zip找到它
By this way for each protobuf defined message, you will be able to parse its instances by doing:
通过这种方式,对于每个 protobuf 定义的消息,您将能够通过执行以下操作来解析其实例:
Foo foo;
const std::string json_foo = "{\"text\": \"Hello world\"}";
protobuf2json::json::parse(foo, json_foo);
It's just an initial implementation, and it just support string fields, but it's easy to implement all kind of fields.
它只是一个初步的实现,它只支持字符串字段,但很容易实现所有类型的字段。