Reputation: 119
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?
Upvotes: 7
Views: 3827
Reputation: 12156
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.
Upvotes: 9
Reputation: 10543
My suggestions in no particular order are:
Use self describing message (at the bottom of the page). In this case you can either
Keep the proto names small and use proto-name in the File name e.g.
salesProto_Store001.bin
This has several good points:
Finally the ProtobufEditor
Supports Self Describing messages where
Has a search function which will try an match the fields in a Protobuf message against known Proto definition files and gives you possible match's
Background: In case you did not know, protocol buffers proto file can be converted to a FileDescriptorSet protocol buffer message and stored
Self describing messagew:
message SelfDescribingMessage {
// Set of .proto files which define the type.
required FileDescriptorSet proto_files = 1;
// Name of the message type. Must be defined by one of the files in
// proto_files.
required string type_name = 2;
// The message data.
required bytes message_data = 3;
}
Upvotes: 2