Open freemin7 opened 1 year ago
Ah, part of the issue here is that StructTypes now has a new struct type SingletonType
that is applied automatically to NoBrainer
, thus resulting in the serialization like "{\"wrap\":\"NoBrainer()\",\"type\":\"brain\"}"
. If you define StructTypes.StructType(::Type{NoBrainer}) = StructTypes.Struct()
, then your example works for me. The other error can be solved by defining an appropriate constructor that takes nothing
, like TypedWrapper{NoBrainer}(::Nothing, type::Symbol) = TypedWrapper{NoBrainer}(NoBrainer(), type)
(we attempted to document this but I know it's a bit buried. The key idea is that if you have structs that can be constructed when fields are missing in the JSON, then you should handle that in a custom constructor).
What's the motivation behind the singleton type?
It helps in other (non-JSON) serialization use-cases and for JSON, provides a more descriptive serialization form (instead of just the default empty object output {}
).
I am not sure which one is more intuitive and should be the default behavior. Do you think better documentation about this should go into JSON3 or StructTypes?
I have a wrapper type which might contains empty object which might be omitted.