C++ and C# proto3 json (de)serialization

843 views
Skip to first unread message

Ron

unread,
Nov 11, 2015, 7:42:22 PM11/11/15
to Protocol Buffers
Hi,

So far I've been using the binary wire format, and now I'm trying to use JSON serialization for some things and the usage seems to be somewhat different, so looking for a little guidance.

In C++, what are the possible ways to go about JSON serialization and deserialization? So far I could only find ways that use the binary wire format as some sort of an intermediate stage between the JSON representation and C++ message objects. Are there any ways of doing this that are more direct, not going through the binary wire format in between? Also, are there any documents and/or examples for using ObjectWriter and other proto3 classes or are they only going to be available once the final release is out? (I saw a post here mentioning it's likely to be sometime around Q4, is that still the case?)

In C# as far as I can tell serialization can be performed using JsonFormatter directly or through ToString() and deserialization is not implemented yet. Is that correct? Will JSON deserialization be available in the final release? For now I'm using a managed wrapper around the C++ JsonToBinary() function as a temporary solution for C# JSON deserialization and it seems to be working okay. Are there any potential pitfalls with this approach I might be overlooking? Are there any alternatives for the time being?

Thanks in advance,
Ron 


Jon Skeet

unread,
Nov 12, 2015, 8:24:43 AM11/12/15
to Protocol Buffers
C# JSON parsing went in a few days ago, actually. There may still be some discrepancies in terms of parsing JSON from third-party sources, but I'd expect parsing the JSON output of the C++ protobuf library to work. If it doesn't, please let me know!

The one bit that *isn't* implemented (in either formatting or parsing) is the Any well-known type. That's the last big feature on my list, I believe.

Jon

Ron

unread,
Nov 23, 2015, 4:25:02 AM11/23/15
to Protocol Buffers
Thanks Jon!
The wrapper around C++ for JSON deserialization seems to be working well so far, but since you say C# JSON deserialization has been implemented in the latest release I'll probably start using that in the near future.


Any advice regarding C++? Anyone has suggestions for JSON serialization and deserialization? Is the proper way going through binary and using the BinaryToJson/JsonToBinary functions? Or is there a more direct way?

Thanks,
Ron

Jon Skeet

unread,
Nov 23, 2015, 7:50:19 AM11/23/15
to Protocol Buffers
I'm afraid I know nothing about the most appropriate way of doing JSON serialization in the C++ code. It would probably be worth specifying exactly what you're trying to do - do you have a message and want to serialize it straight to a stream, or do you want to get the JSON as a std::string or similar?

Jon

Ron

unread,
Nov 23, 2015, 8:17:59 AM11/23/15
to Protocol Buffers
I'm looking to take a protobuf message object and encode it to a string of its protobuf JSON representation, and vice versa.
So far I've been just serializing to protobuf binary format with message.WriteTo(...) or MessageExtensions.ToByteArray(message) and then using BinaryToJsonString(...) or BinaryToJsonStream(...). Just wondering if there's a more direct way of doing this, that doesn't involve the 2 steps (something similar to the C# API's JsonFormatter.Format(message)).

Thanks,
Ron

Ron

unread,
Nov 23, 2015, 10:45:07 AM11/23/15
to Protocol Buffers

On Monday, November 23, 2015 at 3:17:59 PM UTC+2, Ron wrote:
So far I've been just serializing to protobuf binary format with message.WriteTo(...) or MessageExtensions.ToByteArray(message) and then using BinaryToJsonString(...) or BinaryToJsonStream(...)...

 
Oops...
I mean of course the C++ equivalents like message.SerializeToCodedStream(...) or message.SerializeToArray(...), not the highlighted C# functions that I mistakenly mentioned.

D S

unread,
Nov 25, 2015, 6:19:12 PM11/25/15
to Protocol Buffers
I am looking for the same kind of functionality.
Going JSON->Message without binary.
So a new function something like message->ParseFromJson(json)
Want the other way to go with a new function something like MessageToJsonString(..,message,...)

For me, I am using .proto file like a JSON schema and parsing JSON sent over the wire into message objects and vice-versa.
Easy access to JSON elements from the message objects.
Reply all
Reply to author
Forward
0 new messages