StructTypes.jl
StructTypes.jl copied to clipboard
sometimes dicts are passed to constructors as `Dict{String,Any}`
I started out thinking this only happened for nested types, but as I set out to make a MWE, I got even more confused. Here it is
using JSON3, StructTypes
struct Inner
a::Int
end
Inner(dct) = Inner(dct[:a])
StructTypes.StructType(::Type{Inner}) = StructTypes.DictType()
struct Outer
I::Inner
end
Outer(dct) = Outer(dct[:I])
StructTypes.StructType(::Type{Outer}) = StructTypes.DictType()
json = JSON3.write(Dict(:I=>Dict(:a=>1)))
JSON3.read(json, Outer)
gives me
ERROR: LoadError: KeyError: key :I not found
Stacktrace:
[1] getindex at ./dict.jl:467 [inlined]
[2] Outer(::Dict{String,Any}) at /home/expandingman/src/scrap.jl:13 (repeats 2 times)
[3] #construct#2 at /home/expandingman/.julia/packages/StructTypes/CpLmq/src/StructTypes.jl:267 [in
lined]
[4] construct at /home/expandingman/.julia/packages/StructTypes/CpLmq/src/StructTypes.jl:267 [inlin
ed]
[5] read(::StructTypes.DictType, ::Base.CodeUnits{UInt8,String}, ::Int64, ::Int64, ::UInt8, ::Type{
Outer}, ::Type{Symbol}, ::Type{Any}; kw::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),
Tuple{}}}) at /home/expandingman/.julia/packages/JSON3/LtYDK/src/structs.jl:342
[6] read at /home/expandingman/.julia/packages/JSON3/LtYDK/src/structs.jl:285 [inlined]
[7] #read#30 at /home/expandingman/.julia/packages/JSON3/LtYDK/src/structs.jl:279 [inlined]
[8] read at /home/expandingman/.julia/packages/JSON3/LtYDK/src/structs.jl:279 [inlined]
[9] read(::String, ::Type{Outer}; kw::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tu
ple{}}}) at /home/expandingman/.julia/packages/JSON3/LtYDK/src/structs.jl:34
[10] read(::String, ::Type{Outer}) at /home/expandingman/.julia/packages/JSON3/LtYDK/src/structs.jl
:33
[11] top-level scope at /home/expandingman/src/scrap.jl:19
[12] include(::String) at ./client.jl:457
[13] top-level scope at REPL[3]:1
in expression starting at /home/expandingman/src/scrap.jl:19
Usually I would hit this kind of error with Inner where it would try to construct from Dict("a"=>1), I don't know why I suddenly have this problem with the outer one.
As a workaround, it seems that right now one has to define constructors for both Dict{String,Any} and Dict{Symbol,Any} which seems wrong.