bhftbootcamp / Serde.jl

Serde is a Julia library for (de)serializing data to/from various formats. The library offers a simple and concise API for defining custom (de)serialization behavior for user-defined types
Apache License 2.0
31 stars 7 forks source link

Compatible with CSV deserialization of Union{Nothing, Another} type #47

Closed NeroBlackstone closed 1 month ago

NeroBlackstone commented 2 months ago

I noticed that when deserializing the CSV, missing values ​​were assigned the value missing. Of course, this is completely fine and complies with the Julia specification.

But for fields with Union{Nothing, Another} definition, deserialization will report an error:

struct Data
           id::Int
           name::Union{String,Nothing}
end
csv = Serde.to_csv([Data(1,nothing),Data(2,"name2")])
# "id,name\n1,\n2,name2\n"
Serde.deser_csv(Data,csv)
#ERROR: WrongType: for 'Data' value 'missing' has wrong type 'name::Missing', must be 'name::Union{Nothing, String}'

Should we need to compatible with the Nothing type during deserialization, so that fields of type Union{Nothing, Another} can also be successfully deserialized?

I don't know if encouraging such compatibility is a good behavior, but I think that since the type Union{Nothing, Another} field is allowed to be serialized, we should support deserialization of this type, otherwise support of field Union{Nothing, Another} should be removed or throw error when serializing.