Skip to content

Unmarshalling to a Julia type when type has Union Values? #31

@MrDataPsycho

Description

@MrDataPsycho

Hi,
I am trying to unmarshal a json to a Struct type where there is Union in type attributes but it does not work. Is there a way to do that?

That function works:

input = "{ \"id\": 25073877, \"id_str\": \"25073877\"}"

struct UserX
    id::Int64
    id_str::String
end

Unmarshal.unmarshal(UserX, JSON.parse(input))

But Having Union in the struct Marshalling is not working:

struct UserY
    id::Union{Int64, Nothing}
    id_str::Union{String, Nothing}
end

Unmarshal.unmarshal(UserY, JSON.parse(input))

this through following error:

ERROR: MethodError: unmarshal(::Type{Union{Nothing, String}}, ::String, ::Bool, ::Int64) is ambiguous. Candidates:
  unmarshal(DT::Type, parsedJson::String, verbose::Bool, verboseLvl::Int64) in Unmarshal at /home/datapsycho/.julia/packages/Unmarshal/G72dP/src/Unmarshal.jl:38
  unmarshal(::Type{Union{Nothing, T}}, x, verbose::Bool, verboseLvl::Int64) where T in Unmarshal at /home/datapsycho/.julia/packages/Unmarshal/G72dP/src/Unmarshal.jl:232
Possible fix, define
  unmarshal(::Type{Union{Nothing, T}}, ::String, ::Bool, ::Int64) where T

Is it possible to unmarshal such data?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions