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

Time:11-12

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?

CodePudding user response:

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
  • Related