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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:20:17  来源:igfitidea点击:

C++ Protobuf to/from JSON conversion

c++jsonserializationprotocol-buffers

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

pbjsonis another one, which built on rapidjson, maybe faster.

pbjson是另一个,它建立在 Rapidjson 之上,也许更快。

回答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.

它只是一个初步的实现,它只支持字符串字段,但很容易实现所有类型的字段。

回答by Mike Ohlsen

pb2jsonis another C++ library that can do this.

pb2json是另一个可以做到这一点的 C++ 库。