I need to serialize and deserialize a sequence of protocol buffers messages to and from a byte stream. There are a handful of predetermined message types. What is the recommended way of encoding the type information so that my application can know which type it should be reading?
The most common way to do this is to use a union message.
For example:
message AnyMessage {
optional Message1 msg1 = 1;
optional Message2 msg2 = 2;
...
}
Then all messages are encoded/decoded inside an AnyMessage
container. Starting with protobuf 2.6, you can also use the oneof
specifier which will ensure that only one of the submessages is set.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With