Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dict written as a String #328

Open
ZacharieALES opened this issue Jul 31, 2021 · 3 comments
Open

Dict written as a String #328

ZacharieALES opened this issue Jul 31, 2021 · 3 comments

Comments

@ZacharieALES
Copy link

ZacharieALES commented Jul 31, 2021

When I save into a json file a Dict which contains a Dict as a key, the key is replaced by a string.

Here is an example:
d = Dict{Dict{String, Int}, Int}(Dict("v1" => 1) => 2)

I save variable d using

julia> open("test.json", "w") do f
         JSON.print(f, d, 4)
       end

The problem is that I get the following json file:

{
    "Dict(\"v1\" => 1)": 2
}

and when I read the file, due to the quotation marks, Dict(\"v1\" => 1) is considered to be a String rather than a Dict{String, Int}:

julia> sData = join(readlines("test.json"))
julia> dExtracted = JSON.parse(sData)
Dict{String,Any} with 1 entry:
  "Dict(\"v1\" => 1)" => 2

Can I write and read in json a Dict which has a Dict as keys?

@ZacharieALES
Copy link
Author

ZacharieALES commented Jul 31, 2021

After some reading, I found that the name of an object in json is necessarily a string.

I replaced the type Dict{Dict{String, Int} by Vector{Vector{Any}} which is not completely satisfactory but works.

@KristofferC
Copy link
Member

Personally I think JSON.jl should error on such keys and not just naively stringify them.

@ZacharieALES
Copy link
Author

That would have saved me some debugging and reading ^^'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants