C# 中的 Proto2 与 Proto3

Proto2 vs. Proto3 in C#

我必须使用 Google Protocol Buffers 的 proto2 版本向另一个团队发送消息。他们在 Linux 上使用 Java 和 C++。我在 Windows.

上使用 C#

Jon Skeet 的 protobuf-csharp-port (https://github.com/jskeet/protobuf-csharp-port) supports proto2. If I understand correctly, Google has taken this code and folded an updated version of it into the main protobuf project (https://github.com/google/protobuf/tree/master/csharp)。但是它不再支持 C# 的 proto2,只支持 proto3。

我不确定应该使用哪个项目。似乎新的会得到更好的支持(性能,如果其他团队升级的话对 proto3 的支持)。但是我必须将给我的 .proto 文件从 proto2 转换为 proto3,并冒着随之而来的任何问题的风险。

我读到的大部分内容是,proto2 和 proto3 的消息是兼容的。我没有使用 Protocol Buffers 的经验,但我正在使用的 .proto 文件看起来很普通,没有默认值或 oneof 或嵌套任何东西。所以看起来我可以删除他们的 "required" 和 "optional" 关键字并使用新库,将其视为 proto3 文件。

您认为使用更新的库值得吗?是否有会使 proto2 和 proto3 消息不兼容的 proto 功能列表?

如果另一方团队有任何必填字段,而您在未指定这些字段(或什至明确指定原语的默认值)的情况下向他们发送消息,则另一端将无法接收消息 - 他们不会验证。

proto2 和 proto3 之间存在各种差异 - releases page 中列出了一些差异:

The following are the main new features in language version 3:

  • Removal of field presence logic for primitive value fields, removal of required fields, and removal of default values. This makes proto3 significantly easier to implement with open struct representations, as in languages like Android Java, Objective C, or Go.
  • Removal of unknown fields.
  • Removal of extensions, which are instead replaced by a new standard type called Any.
  • Fix semantics for unknown enum values.
  • Addition of maps.
  • Addition of a small set of standard types for representation of time, dynamic data, etc.
  • A well-defined encoding in JSON as an alternative to binary proto encoding.

未知字段的删除对您来说可能是一个重大问题 - 如果其他团队希望能够向您发送包含您的代码不知道的某些字段的消息,并且您能够 return给他们维护这些字段的消息,proto3 可能会给你带来问题。

如果您可以使用 proto3,我建议使用 proto3 版本,部分原因是它会得到适当的支持,而 proto2 版本基本上处于维护模式。两者之间存在显着差异,主要是在可变性方面 - proto3 代码库中生成的消息 类 是可变的,这对于即时可用性非常有用,但可能会在其他方面带来挑战。