Bug on JSON3 serialization?

Could someone clarify whether this is a bug or I’m doing something wrong?

import JSON3
d = Dict{Tuple{Int,String},Float64}()
d[(10,"a")] = 1.
d[(20,"b")] = 2.
JSON3.write("/tmp/temp.json", d)
JSON3.read("/tmp/temp.json", Dict{Tuple{Int,String},Float64})

The last line throws

ERROR: MethodError: Cannot `convert` an object of type Char to an object of type String
The function `convert` exists, but no method is defined for this combination of argument types.

That object is serialized in the following way:

{"(10, \"a\")":1.0,"(20, \"b\")":2.0}

What’s happening is that JSON3 sees the key "(10, \"a\")" and tries pass it to the dictionary key type:

Tuple{Int,String}("(10, \"a\")")  # ERROR: MethodError: Cannot `convert` an object of type Char to an object of type String

@quinnj

Ok as soon as I posted this I’ve found that this is known and someone posted a workaround using wrappers.

Yeah, non-string keys were never officially supported or thought about, so no official work-around or support currently.

I think that should probably error on write rather than on read, though, since it shouldn’t be able to do that.

2 Likes