Protocol buffer v3 claims, that library is json friendly (https://developers.google.com/protocol-buffers/docs/proto3#json), but I cannot find how to achieve get that mapping. Should I add some plugin, or some option into protoc, or call something special instead SerializeTo/ParseFrom?
Is it someone who use that feature?
I'm using Protobuf 3.3.0, which does have a built-in JSON serializer and parser. You can use 2 functions from
google/protobuf/util/json_util.h
calledMessageToJsonString()
andJsonStringToMessage()
to make your C++ generatedMessage
objects go to and from JSON respectively.Here's a simple test that uses them:
test-protobuf.proto
:test-protobuf.cpp
:You can compile these files (assuming that you have protobuf, a compiler, and CMake installed) by using the following
CMakeLists.txt
file (tested on Windows).Assuming that
CMakeLists.txt
,test-protobuf.proto
, andtest-protobuf.cpp
are in the same directory, here are the commands to compile and run them on Windows with Visual Studio 15 2017 and 64-bit protobuf libraries.You should see the following output:
Protobuf has json api for C#. There are some json class for C# in google protobuf reference and You can find some tests in github protobuf repository for java and c++.