Skip to content

convert(::Type{Dict{Symbol,Any}}, ::JSON.Object) fails #19

@SteffenPL

Description

@SteffenPL

For dictionaries which use Symbols as keys, the convert function fails.
Here is a minimal example to reproduce the error message

Dict{Symbol,Int64}( LazyJSON.value("""{"a":10}""") )

Output:

ERROR: ArgumentError: "a" is not a valid key for type Symbol
Stacktrace:
 [1] setindex!(::Dict{Symbol,Int64}, ::LazyJSON.Number{String}, ::LazyJSON.String{String}) at ./dict.jl:374
 [2] Dict{Symbol,Int64}(::LazyJSON.Object{Nothing,String}) at ./dict.jl:103
 [3] top-level scope at none:0

I am no Julia expert. Should it remain like this or would it be possible to add Symbols as supported keys?

Anyway, my workaround is to define

import Base.setindex!
function setindex!(d::Dict{Symbol,T}, v::LT, k::LazyJSON.String{String}) where {T, LT}
    setindex!(d,v,Symbol(k))
end

such that every LazyJSON.String is first converted to a Symbol.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions