Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proto2 vs. Proto3 in C#

I have to send messages to another team using the proto2 version of Google Protocol Buffers. They are using Java and C++ on Linux. I'm using C# on Windows.

Jon Skeet's 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). But it no longer supports proto2 for C#, only proto3.

I'm not sure which project I should use. It seems like the new one will be better supported (performance, support for proto3 if the other team ever upgrades). But I would have to convert the .proto file that I was given from proto2 to proto3 and risk any issues that come with that.

I've read that for the most part, the messages for proto2 and proto3 are compatible. I have no experience with Protocol Buffers, but the .proto file I'm working with looks pretty vanilla, no default values or oneof or nested anything. So it seems like I could just delete their "required" and "optional" keywords and use the new library, treating this as a proto3 file.

In your opinion, is it worth the hassle to use the newer library? Is there a list of proto features that would make the proto2 and proto3 messages incompatible?

like image 568
Michael Covelli Avatar asked Apr 20 '16 11:04

Michael Covelli


People also ask

What is the difference between proto2 and proto3?

Proto3 is the latest version of Protocol Buffers and includes the following changes from proto2: Field presence, also known as hasField , is removed by default for primitive fields. An unset primitive field has a language-defined default value.

Why is proto3 better than proto2?

The Proto3 syntax contains the addition of many new features that were not present in the Proto2 syntax, as well as the removal of some existing features. These changes include: Removal of required fields. Removal of default values.

Are proto2 and proto3 compatible?

Using proto2 Message TypesIt's possible to import proto2 message types and use them in your proto3 messages, and vice versa. However, proto2 enums cannot be used directly in proto3 syntax (it's okay if an imported proto2 message uses them).

Is proto2 deprecated?

Actually we have no plans to deprecate proto2 and we are still actively developing it, so you can really choose either one without having to worry about support going away.


1 Answers

If the other team has any required fields and you send messages to them without specifying those fields (or even explicitly specifying the default value, for primitives) then the other end will fail to receive the messages - they won't validate.

There are various differences between proto2 and proto3 - some are listed on the 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.

The removal of unknown fields could be a significant issue to you - if the other team expects to be able to send you a message with some fields your code is unaware of, and you be able to return a message to them maintaining those fields, proto3 could pose problems for you.

If you can use proto3, I'd suggest using proto3 version, partly as it will have proper support whereas the proto2 version is basically in maintenance mode. There are significant differences between the two, primarily in terms of mutability - the generated message classes in the proto3 codebase are mutable, which is great for immediate usability, but can pose challenges in other areas.

like image 94
Jon Skeet Avatar answered Oct 23 '22 13:10

Jon Skeet