JSONParser in Google protobuf is not parsing byteString value properly

793 views
Skip to first unread message

arun kumar

unread,
Sep 6, 2019, 2:45:15 AM9/6/19
to Protocol Buffers
I have JSON data like below,

{
 
"clientIdentifier": {
   
"context": "CFID",
   
"value": "12340000"
 
},
 
"desiredSessionTimeout": "30000"
}

Value 12340000 is the datatype of  " ByteString " as per proto 

When I am trying to parse using JSON parser 

Google.Protobuf.JsonParser parser = Google.Protobuf.JsonParser.Default;
IMessage imesg= parser.Parse(json, MessageDescriptor);

imesg is getting created but the value is being return as 12340000. Because of this , serialized data is showing wrong value.  I tried JSONParser settings as well. But I couldnt get the desired result..  

I got struck at this point. Please help me out 

Thanks in Advance,
Arun

Nadav Samet

unread,
Sep 6, 2019, 11:18:10 AM9/6/19
to Protocol Buffers
Note that the JSON protobuf format represents bytes as base64 string: https://developers.google.com/protocol-buffers/docs/proto3#json
I think you assume the bytes are going to be represented as an hexstring in the json, given your earlier question.

-Nadav

Arun

unread,
Sep 9, 2019, 1:18:28 AM9/9/19
to Nadav Samet, Protocol Buffers
Thanks Nadav..! Perfect.!  This is what causing the problem.   In Json, value will be always passed as hexString like 12340000.  Do I need to change the json value to base64 encoded string as intermediate layer and then use the Google parser?  What will be your suggestion. ? 

Thanks Nadav  

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/37f435a7-3650-419e-8deb-0478f4f11c69%40googlegroups.com.


--
-- Arun

Nadav Samet

unread,
Sep 9, 2019, 1:28:40 AM9/9/19
to Arun, Protocol Buffers
I am not fully aware of what changes you can make in your system. You might be able to do any of the following:
1. change the sending side to use base64 .
2. on the receiving side, convert hex to base64 before calling the Google parser (this is what you suggested above).
3. change the type of the field in the protobuf from bytes to string, and convert the hex string to bytes in your own code after the json is parsed. This is a change in the binary representation, so if you have existing messages you persisted somewhere using the old format, that would no't work.

-Nadav

--
-Nadav

Arun

unread,
Sep 9, 2019, 1:58:40 AM9/9/19
to Nadav Samet, Protocol Buffers
Thanks a lot .. I am planning to try 1 and 2..  Messages would come from different source, some are binary data . So, option 3 will mess up.  Thanks a lot Nadav
--
-- Arun

Reply all
Reply to author
Forward
0 new messages