Alperen AYDIN
Alperen AYDIN

Reputation: 567

Does repeated fields in protobuffers keep the order they are inserted?

Consider the following message.

message example { 
   repeated string text; 
}

Let's say that in C++ I have a list of string that I insert into the text field of example:

exemple aMessage; 
std::list<std::string> aList = ... ;
for (std::string anStr : aList) 
{
    aMessage.add_text(anStr);
}

Later on if I access text of the my message, will that field be ordered in the same way as my list? What about when I serialize it and send it off somewhere?

Will the order stay constant?

Upvotes: 17

Views: 14422

Answers (1)

jpa
jpa

Reputation: 12176

Yes, repeated fields retain the order of items.

From Google's Protocol Buffers encoding specification:

The order of the elements with respect to each other is preserved when parsing, though the ordering with respect to other fields is lost.

Upvotes: 23

Related Questions