Open chnlkw opened 5 years ago
Sorry for the delay in replying...
FSharp.Reflection.FSharpType.IsUnion(SimpleUnion.A.GetType())
returns true
, so DiscremenatedUnionResolver
will attempt to generate a formatter.
However, SimpleUnion.A.GetType()
returns type info of the concrete class generated by the compiler, MessagePackSerializer requires IMessagePackFormatter<SimpleUnion.A>
.
So far I have not found a way to solve this problem within DiscremenatedUnionResolver
.
Please try to check and use Type.BaseType
:
let t =
let t = A.GetType()
match t.BaseType with
| null -> t
| p when Reflection.FSharpType.IsUnion(p) -> p
| _ -> t
MessagePackSerializer.NonGeneric.Serialize(t, A, resolver)
Problem
I get an error when serializing
A
inSimpleUnion
The exception is
But it can serialize
A
correctly withSimpleUnion2
which inserts aunit
field.Here is my test case source
Motivation
I am trying to porting this extension to Akka.Serialization.MessagePack, which uses MessagePackSerializer.NonGeneric to serialize a object. This serialize function needs a given type value, which is get from
obj.GetType()
in Akka implementation. I use the serializaion here