Using the Protobuf-Net ProtoMember Attribute "IsRequired"

What is the use of the Protobuf-Net ProtoMember attribute "IsRequired"?

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 the value (since it does not exist), or should I set the value to false? And if I set to false, will the value be serialized correctly?

+6
source share
1 answer

According to Problem 262: ProtoMembers with IsRequired = true are not really needed :

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

When IsRequired is false (the default value), the default values ​​are not serialized, for example. the integer value 0 will not be serialized, the value is 1.

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

As a result of deserialization using protobuf-net, the value of the IsRequired attribute does not change. However, if you use 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 the serialized file written before the property was added, the new property simply will not be set.

+6
source

Source: https://habr.com/ru/post/895919/


All Articles