简体   繁体   English

protobuf 消息字段 id 和字段顺序是什么关系?

[英]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.我想从 protobuf 和序列化/反序列化的角度了解以下消息是否兼容。

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序列化程序应该(但不是必须)以数字字段的升序写入字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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