Protobuf-net UseImplicitZeroDefaults and Enum by default

We are trying to use protobuf-net, but we cannot understand the UseImplicitZeroDefaults that we have now disabled in the custom RuntimeTypeModel. Initially, we used the standard RuntimeTypeModel, but noticed that the boolean properties are not cloned, even if the DefaultValue parameter is set, i.e. DefaultValue = true, but when set to false, the cloned property will always be true.

We solved this by creating a custom RuntimeTypeModel that allowed us to set UseImplicitZeroDefaults to false. But setting this to false causes the following error:

ProtoBuf.ProtoException: No wire-value is mapped to the enum 

Please note that some of our enumerations are based on a non-zero value, can this be a problem? How can we clone / serialize logical properties and enumerations (a mixture of nonzero and null)?

Edit: I used some information found at protobuf-net enum serialization and can report:

 [ProtoMember(10), DefaultValue(SiteType.Partition)] public SiteType Type { get; set; } 

It still results in a "No wire value" error.

 [ProtoMember(10, IsRequired = true)] public SiteType Type { get; set; } 

It still results in a "No wire value" error.

 public enum SiteType { Error = 0, ... 

This works, but ideally we would like to keep our listing clean.
Perhaps a cleaner way to specify a default value:

 [DefaultValue(SiteType.Server)] public enum SiteType { Server = 1, Monkey = 2 ... 
+2
source share
1 answer

We solved this problem by specifying a default enumeration for any non-zero enumerations. We specified the default value in the constructor of the serializable class. This was by far the most sophisticated solution and did not require additional protobuf-net attributes.

In addition, it makes sense to explicitly set the default value for enum properties based on a zero value.

+3
source

All Articles