8

What is the usage for the Protobuf-Net ProtoMember "IsRequired" attribute?

What effect will it have if I add a new property (member) to an already serialized class in a file. If I use "IsRequired=true" will it just accept it without loading a value (since it's not there) or should I set the value to false? And if I set it to false, will the value be serialized properly?

Eliseo
  • 1,547
  • 1
  • 15
  • 14

1 Answers1

7

According to Issue 262: ProtoMembers with IsRequired=true aren't really required:

Currently, IsRequired primarily impacts serialization, forcing it to ignore some default rules.

When IsRequired is false (the default), default values are not serialized, e.g. an integer value of 0 would not be serialized, a value of 1 would.

When IsRequired is true, all values, including default values, are serialized.

Deserialization using protobuf-net currently seems unaffected by the the value of the IsRequired attribute. However if you are using a different implementation of Google Protocol Buffers for deserialization you may see different behavior.

If you add a new property to an existing class, and deserialize a serialized file written before the property was added, the new property will simply not be set.

Phillip Trelford
  • 6,513
  • 25
  • 40