protobuf-net UseImplicitZeroDefaults and Enum defaults

南楼画角 提交于 2019-12-24 02:28:11

问题


We're trying to use protobuf-net but having trouble understanding UseImplicitZeroDefaults which we now disable in a custom RuntimeTypeModel. We initially used the default RuntimeTypeModel but noticed boolean properties were not being cloned even though a DefaultValue was being specified, i.e. DefaultValue=true but when set to false the cloned property would always be true.

We resolved this by creating a custom RuntimeTypeModel which has allowed us to set UseImplicitZeroDefaults to false. But setting this to false is causing the following error;

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

Note that some of our enums are non-zero based, could this be causing an issue? How can we clone/serialize boolean properties and enums (mixture of non-zero and zero based)?

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

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

Still results in the "No wire-value" error.

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

Still results in the "No wire-value" error.

public enum SiteType
{
    Error = 0,
    ...

This works but ideally we would like to leave our enum clean.
Perhaps a cleaner way to specify the default value:

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

回答1:


We resolved this issue by specifying a default enum for any non-zero based enums. We specified the default in the constructor of the class being serialized. This was by far the tidiest solution and didn't require any additional protobuf-net attributes.

Plus, it made sense to explicitly set a default value for non-zero based enum properties.



来源:https://stackoverflow.com/questions/13887553/protobuf-net-useimplicitzerodefaults-and-enum-defaults

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!