简体   繁体   中英

What is the relation of protobuf message field id and field order?

I want to understand if the messages bellow are compatible from the perspective of protobuf and serialization/deserialization.

message HelloReply {
  string message = 1;
  string personalized_message = 2;
}
message HelloReply {
  string personalized_message = 2;
  string message = 1;
}

Does the order matter for compatibility in any situation?

The textual order is largely irrelevant, although it may impact some code generation tooling - but most languages don't care about declaration order, so even that: won't matter. The fields are still defined semantically equivalent - the numbers match the existing meaning (name) and type. It is the number that is the determining feature in identifying a field.

At the protocol level:

  • parsers must allow fields in any order
  • serializers should (but not must ) write fields in ascending numerical field order

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM