2

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
    ...
Community
  • 1
  • 1
paligap
  • 942
  • 1
  • 12
  • 28
  • I'll have to take a look - too late for tht tonight; but: does it work if you set IsRequired=true? – Marc Gravell Dec 14 '12 at 23:46
  • IsRequired is set at the property level however to avoid over decoration of our classes we use [ProtoContract(ImplicitFields = ImplicitFields.AllPublic)]. So we're not setting anything at the property level, perhaps we should! – paligap Dec 14 '12 at 23:59
  • I will investigate, to see if something is wrong here – Marc Gravell Dec 15 '12 at 08:08
  • Marc, I updated my question to reflect the results from a few tests. Looking forward to your feedback. – paligap Dec 16 '12 at 11:37
  • Marc, just to add I was able to resolve this issue but setting a default enum value in the constructor of the class object to be serialized. This resolved the issue and avoids us having to use a protobuf-net attribute on the enum property. – paligap Dec 17 '12 at 12:37

1 Answers1

3

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.

paligap
  • 942
  • 1
  • 12
  • 28