When parsing a custom type with generically defined StructUtils.StructStyle methods, trying to parse said type with a custom JSON.JSONStyle results in a method ambiguity.
Minimal reproducer (JSON v1.6.1, StructUtils 2.8.2, julia 1.12.5, M1 Macbook Pro running MacOS 13.5.1)
module M
using StructUtils
struct A
v::Dict{String, Int}
end
Base.pairs(a::A) = Base.pairs(a.v)
StructUtils.initialize(::StructUtils.StructStyle, ::Type{A}, source) = A(Dict{String, Int}())
StructUtils.dictlike(::StructUtils.StructStyle, ::Type{A}) = true
StructUtils.addkeyval!(a, k, v) = a.v[k] = v
end
a = M.A(Dict("a" => 1, "b" => 2))
using JSON
struct CustomJSONStyle <: JSON.JSONStyle end
style = CustomJSONStyle()
json_str = JSON.json(a; style)
JSON.parse(json_str, M.A; style)
Output
julia> JSON.parse(json_str, M.A; style)
ERROR: MethodError: dictlike(::JSON.JSONReadStyle{JSON.Object{String, Any}, Nothing, CustomJSONStyle}, ::Type{Main.M.A}) is ambiguous.
Candidates:
dictlike(st::JSON.JSONReadStyle, ::Type{T}) where T
@ JSON ~/.julia/packages/JSON/gMdcG/src/parse.jl:177
dictlike(::StructUtils.StructStyle, ::Type{Main.M.A})
@ Main.M REPL[1]:10
Possible fix, define
dictlike(::JSON.JSONReadStyle, ::Type{Main.M.A})
When parsing a custom type with generically defined
StructUtils.StructStylemethods, trying to parse said type with a customJSON.JSONStyleresults in a method ambiguity.Minimal reproducer (JSON v1.6.1, StructUtils 2.8.2, julia 1.12.5, M1 Macbook Pro running MacOS 13.5.1)
Output