Thoth.Json.Net icon indicating copy to clipboard operation
Thoth.Json.Net copied to clipboard

Cannot serialized higher order type when type is boxed

Open bruno-cadorette opened this issue 5 years ago • 3 comments

This code throws System.ArgumentException: 'Could not determine JSON object type for type Program+MyRecord.'

type MyRecord = {
    Field1 : string
    Field2 : int
    Field3 : bool
}
type Wrapper<'a> = {
    Id:     string
    Config: 'a
}
let myObject = {
    Id = "test test test";
    Config = (box {Field1 = "hi"; Field2 = 2; Field3 = true })
}
let result = JsonConvert.SerializeObject(myObject, Converters.Converter())

If we remove the box, the serialization works but sadly in my case I cannot avoid it.

bruno-cadorette avatar Mar 30 '20 21:03 bruno-cadorette

Hello,

at first, I would have say that boxing the type would prevent us to access the "real" type info. But the error seems to know that it's the type Program+MyRecord that need to be accessed.

Will have to dig more in it.

MangelMaxime avatar Mar 31 '20 08:03 MangelMaxime

Thanks!

Line 416 of Encode.fs it call autoEncoder with fi.PropertyType which returns System.Object, which ultimately create this encoder: boxEncoder(fun (v: obj) -> JValue(v) :> JsonValue)

I think this happen because the type of the Config field when we box it is object.

I might have a fix

bruno-cadorette avatar Mar 31 '20 14:03 bruno-cadorette

This indeed looks like the right direction

MangelMaxime avatar Mar 31 '20 14:03 MangelMaxime