Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 848
Alperen AYDIN Avatar asked Mar 08 '19 12:03

Alperen AYDIN


Video Answer


1 Answers

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.

like image 171
jpa Avatar answered Sep 19 '22 20:09

jpa