Protobuf

Backwards Compatibility (proto3)

  • Don’t change the field numbers for any existing fields.
  • If you add new fields, any messages serialized by code using your “old” message format can still be parsed by your new generated code.
  • You should keep in mind the default values for these elements so that new code can properly interact with messages generated by old code. Similarly, messages created by your new code can be parsed by your old code: old binaries simply ignore the new field when parsing
  • Fields can be removed, as long as the field number is not used again in your updated message type.
  • Changing a single value into a member of a new oneof is safe and binary compatible.

Default Values (proto3)

The optional keyword is not required, but fields automatically default to a certain type-specific value:

  • For strings, the default value is the empty string.
  • For bytes, the default value is empty bytes.
  • For bools, the default value is false.
  • For numeric types, the default value is zero.
  • For enums, the default value is the first defined enum value, which must be 0.
  • For message fields, the field is not set.

oneof

If you have a message with many optional fields and where at most one field will be set at the same time, you can enforce this behavior and save memory by using the oneof feature.

Java Implementation

  • Use Builder classes to build up protobuf data objects.
Edit