mbraceproject / FsPickler

A fast multi-format message serializer for .NET
http://mbraceproject.github.io/FsPickler/
MIT License
324 stars 52 forks source link

Wrong serialization/deserialization enums which has DataContract attribute applied #77

Closed vasily-kirichenko closed 7 years ago

vasily-kirichenko commented 7 years ago
[<DataContract>]
type E =
    | C1 = 1uy
    | C2 = 2uy
    | C3 = 3uy
    | C4 = 4uy

let p = FsPickler.CreateBinarySerializer()
let ms = new MemoryStream()
//let action = TfpAction.Delete
let x = E.C2
p.Serialize(ms, x, leaveOpen = true)
ms.Position <- 0L
let x': E = p.Deserialize ms
// val x' : E = 0
x' = x
// false

Removing DataContract attribute solves the problem.

eiriktsarpalis commented 7 years ago

Interesting. I wasn't aware that DataContract applies to enumerations, which occurs through use of the EnumMember attribute. DataContract-annotated enums will fail serialization if attempting to write a value that does not specify an EnumMember attribute. In other words, "correct behaviour" in the context of your code would result in a serialization exception.

I must say I dislike this design, and it's a good question whether FsPickler should attempt to replicate this behaviour or just ignore the attributes altogether.