Skip to content

Parsing JSON ignores StructUtils methods for custom JSONStyle #453

@curtd

Description

@curtd

JSON v1.5.1 internally enforces the use of JSONReadStyle, which doesn't forward the StructUtils API to the corresponding methods defined for custom JSONStyle, breaking previous deserialization behaviour from JSON 1.5.0.

Minimal reproducing example

       import JSON, StructUtils

       @kwdef struct A 
            vals::Dict{String, Int} = Dict{String, Int}()
       end
       Base.keytype(::A) = String
       Base.valtype(::A) = Int
       StructUtils.addkeyval!(a::A, k, v) = StructUtils.addkeyval!(a.vals, k, v)
       struct CustomJSONStyle <: JSON.JSONStyle end 
       StructUtils.dictlike(::CustomJSONStyle, ::Type{A}) = true 

       str = """{"a": 1, "b": 2 }"""

On JSON 1.5.0

julia> JSON.parse(str, A; style=CustomJSONStyle())
A(Dict("b" => 2, "a" => 1))

On JSON 1.5.1

julia> JSON.parse(str, A; style=CustomJSONStyle())
ERROR: TypeError: in typeassert, expected Dict{String, Int64}, got a value of type Nothing
Stacktrace:
 [1] macro expansion
   @ ~/.julia/packages/StructUtils/RFX5w/src/StructUtils.jl:-1 [inlined]
 [2] _construct
   @ ~/.julia/packages/StructUtils/RFX5w/src/StructUtils.jl:1154 [inlined]
 [3] makestruct
   @ ~/.julia/packages/StructUtils/RFX5w/src/StructUtils.jl:1176 [inlined]
 [4] make
   @ ~/.julia/packages/StructUtils/RFX5w/src/StructUtils.jl:942 [inlined]
 [5] _parse
   @ ~/.julia/packages/JSON/5986W/src/parse.jl:223 [inlined]
 [6] parse(x::JSON.LazyValue{…}, ::Type{…}; dicttype::Type{…}, null::Nothing, style::CustomJSONStyle, unknown_fields::Symbol)
   @ JSON ~/.julia/packages/JSON/5986W/src/parse.jl:217
 [7] parse
   @ ~/.julia/packages/JSON/5986W/src/parse.jl:217 [inlined]
 [8] #parse#52
   @ ~/.julia/packages/JSON/5986W/src/parse.jl:207 [inlined]
 [9] top-level scope
   @ REPL[12]:1
Some type information was truncated. Use `show(err)` to see complete types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions