Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protobuf-Net ProtoMember "IsRequired" attribute usage

Tags:

protobuf-net

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?

like image 530
Eliseo Avatar asked Aug 25 '11 20:08

Eliseo


1 Answers

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.

like image 89
Phillip Trelford Avatar answered Jan 02 '23 18:01

Phillip Trelford