JuliaIO / JuliaIO/JSON.jl

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

Open
#464 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
360
Forks
113
Avg merge
5d 2h
Merged PRs (30d)
4

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at parse.jl:177 and inspect how JSON.JSONReadStyle dispatches dictlike for custom types alongside the StructUtils.StructStyle method in the reproducer. Run the supplied Julia example and relevant package tests; done means parsing M.A with a custom JSONStyle no longer raises an ambiguous-method error.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.