Skip to content

Method ambiguity for custom types with generically defined StructUtils.StructStyle methods #464

Description

@curtd

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})

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