.Net Protocol Buffers to JSON, JsonFormatReader class does not handle outmost curly braces? -
i working on using google protocol buffers, using protobuf-csharp-port library (https://code.google.com/p/protobuf-csharp-port/). google.protocolbuffers.serialization class has jsonformatreader / jsonformatwriter class, when use them not place beginning , ending curly braces json document, nor can read same document write if add beginning , ending braces.
so example calling
pb.protobufmessage message = createmymessage(); string json; using (stringwriter sw = new stringwriter()) { icodedoutputstream output = jsonformatwriter.createinstance(sw); message.writeto(output); output.flush(); json = sw.tostring(); } creates:
"\"field1\":\"prop1\",\"field2\":1,\"subitem\":{\"x\":0,\"y\":0,\"z\":0}" if try parse
string jsonmessage = "{\"field1\":\"prop1\",\"field2\":1,\"subitem\":{\"x\":0,\"y\":0,\"z\":0}}" using
pb.protobufmessage copy; icodedinputstream input = jsonformatreader.createinstance(jsonmessage); copy = pb.protobufmessage.createbuilder().mergefrom(input).build(); i following:
(1:1) error: unexpected token '{', expected: '"'. @ google.protocolbuffers.serialization.jsoncursor.assert(boolean cond, char expected) @ google.protocolbuffers.serialization.jsoncursor.consume(char ch) @ google.protocolbuffers.serialization.jsoncursor.readstring() @ google.protocolbuffers.serialization.jsonformatreader.peeknext(string& field) @ google.protocolbuffers.serialization.abstractreader.google.protocolbuffers.icodedinputstream.readtag(uint32& fieldtag, string& fieldname) @ ... why { } missing, , valid json?
you need write/read message start/end. like:
output.writemessagestart(); message.writeto(output); output.writemessageend(); similary wilhe reading:
input.readmessagestart(); builder.mergefrom(input); input.readmessageend(); the above code works json , binary reader/writers.
Comments
Post a Comment