Is anyone familiar with a stable C++ solution (library, code snippet etc.) which converts protobuf messages to/from JSON?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
This one is better IMO: https://github.com/shramov/json2pb
it does conversion in both directions and handles extensions
回答2:
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.
You can find it at 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:
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.
回答3:
pbjson is another one, which built on rapidjson, maybe faster.
回答4:
pb2json is another C++ library that can do this.