tonynhan / protobuf-net

Automatically exported from code.google.com/p/protobuf-net
Other
0 stars 0 forks source link

Error while serializing an enum with [Flags] attribute. #79

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Declare an enum with the [Flags] attribute.
2. Serialize an enum value using a combined flag.

What is the expected output? What do you see instead?
Serialization should complete but an error is thrown instead:
The value (MyEnum.MyValue) has no wire-representation for property MyProperty

What version of the product are you using? On what operating system?
1.0.0.262 on Windows XP SP3

Please provide any additional information below.
See
http://stackoverflow.com/questions/1494939/error-while-using-protobuf-net-with-f
lags-enum

Original issue reported on code.google.com by stecy.d...@gmail.com on 29 Sep 2009 at 9:15

GoogleCodeExporter commented 8 years ago
Hi; I'll update SO later (work time etc), but ultimately the protocol buffers 
wire 
format doesn't provide any scope for [Flags] enums - it enforces enum values 
against 
the discreet set. I could allow this easily enugh, but:

 - I'd probably have to disable enum mappings in this case, or do a lot of ugly bit-
matching work
 - it would not be strictly compatible

An easier way of doing this may be to do a shim in your code:

    public MyEnum MyValue {get;set;}
    [ProtoMember(12)]
    private int MyValueWire {
        get {return (int)MyValue;}
        set {MyValue = (MyEnum)value;}
    }

The other alternative would be to add a flag that works like the above on your 
behalf; treating it as an int rather than an enum.

Original comment by marc.gravell on 30 Sep 2009 at 7:07

GoogleCodeExporter commented 8 years ago
Hi Marc,

Thanks for the tips. However, would it be possible to bump up the priority on 
this one?

I wouldn't mind having a flag that would auto-cast the enum to an int.

Original comment by stecy.d...@gmail.com on 8 Oct 2009 at 7:55

GoogleCodeExporter commented 8 years ago
I'll see if I can add something to MemberSerializationOptions

Original comment by marc.gravell on 9 Oct 2009 at 5:51

GoogleCodeExporter commented 8 years ago
Actually, I guess I could apply this by default for [Flags] enums (since such 
enums 
won't really work at the minute, it isn't a huge breaking change - and even 
then it 
assumes no [ProtoEnum] overrides, so a corner-case when it breaks)

Original comment by marc.gravell on 9 Oct 2009 at 5:53

GoogleCodeExporter commented 8 years ago
Mark with DataFormat = DataFormat.TwosComplement, in r274

Original comment by marc.gravell on 9 Oct 2009 at 6:37

GoogleCodeExporter commented 8 years ago
So quick!

Thanks, I'll be trying this next week.

Original comment by stecy.d...@gmail.com on 9 Oct 2009 at 9:35