0

I have two protos like lets say

Message1 and Message2.

I will get these proto messages. I want to serialize them but append the serialized bytes to a single byte array. What is the proper way to do this?

I mean from the resultant byte array, the consumer should be able to deserialize the two messages. Do I need to add some kind of metadata like

Message1 -> 1
Message2 -> 2

Then when I append the bytes obtained for the individual serialized messages(Message1 and Message2 in this case) to something

result = [1] ,[len of bytearray of message1] [bytearray for message1], [2], [len of bytearray for message2], [bytearray for message2]

and then send the bytearray. At the consumer end they will read the first byte as the message type, the second 4 bytes for the message length and based upon that read the first message bytearray and so on.

How about if I want to serialize them to json. Do I still need to do some kind of encoding?

Also lets simplify the question. If I have to add multiple instances of the same message in a single byte array, what would be the ideal way to do it?

rajan sthapit
  • 4,194
  • 10
  • 42
  • 66
  • How is your serialize/parse pattern? Do you need parse the message one by one (like from the network) or you only need to parse all the messages at once(like from the diskfile)? – Yi Zhenfei Aug 29 '18 at 07:06

1 Answers1

2

You're right to be considering some scheme to indicate which bytes in the array belong to which message, and what each message type is. Protobuf wireformat itself has no message type or length demarcation.

However, there is one option. If you have a third message that is a oneof of the first two, and a fourth that is an array of the third, all you need to do is populate the fourth message type.

message Message1
{
    ...
}

message Message2
{
    ...
}

message Message3
{
    oneof type
    {
        Message1 msg1 = 1;
        Message2 msg2 = 2;
    }
}

message Message4
{
    repeated Message3 messages = 1;
}

Assuming the sending of the byte array doesn't itself need to be demarcated (i.e. you're not sending through a socket or other byte stream), you need do nothing else than send just Message4's.

bazza
  • 7,580
  • 15
  • 22