Parse sub message from binary encoding

36 views
Skip to first unread message

R P

unread,
May 25, 2020, 5:28:35 AM5/25/20
to Protocol Buffers
Hello

I want to parse a sub message from a binary super message.

I have found some useful information about message encoding here:

From the link:
message Test1 { optional int32 a = 1; }
message
Test3 { optional Test1 c = 3; }

Let's say i have a binary representation of a Test3 message with the sub message field 'a' is set to 150:
1a 03 08 96 01

Is there a method to parse Test1 message from this binary encoding?

BR
Rainer

Adam Cozzette

unread,
May 26, 2020, 11:53:10 AM5/26/20
to R P, Protocol Buffers
Why not just parse the entire parent message? But if you really want to avoid that for some reason, probably the easiest approach is to parse the binary as an EmptyMessage, then look through the unknown fields for your submessage field and parse the submessage from there.

--
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/24115597-43bf-4262-86e4-a861ccd13e21%40googlegroups.com.

R P

unread,
May 28, 2020, 5:00:58 AM5/28/20
to Protocol Buffers
Appreciate your answer!

I've no deep insights into protobuf. What happens if i parse a binary as EmptyMessage? Is it less costly than parsing as a concrete message (e.g. Test3 from the example)?

BR
Rainer

Adam Cozzette

unread,
May 28, 2020, 9:01:51 AM5/28/20
to R P, Protocol Buffers
I'm not sure which is cheaper, and it might depend on the situation. But unless you're trying to do something unusual, it's probably best to just parse the parent message normally and access the submessage from there.

--
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.
Reply all
Reply to author
Forward
0 new messages