问题
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:
pbjson is another one, which built on rapidjson, maybe faster.
回答3:
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.
回答4:
pb2json is another C++ library that can do this.
来源:https://stackoverflow.com/questions/7007876/c-protobuf-to-from-json-conversion