You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
See the MWE below. I would have expected that when I read the object back in, the keys would have been Foo("a") and Foo("c"). Instead, they are Foo("Foo(\"a\")") and Foo("Foo(\"c\")"), which is not correct.
MWE:
julia>import JSON3
julia>import StructTypes
julia>mutable struct Foo
value::StringFoo() =new()
Foo(value) =new(value)
end
julia>mutable struct Bar
value::StringBar() =new()
Bar(value) =new(value)
end
julia>mutable struct Baz
dict::Dict{Foo, Bar}Baz() =new()
Baz(dict) =new(dict)
end
julia> StructTypes.StructType(::Type{Foo}) = StructTypes.Mutable()
julia> StructTypes.StructType(::Type{Bar}) = StructTypes.Mutable()
julia> StructTypes.StructType(::Type{Baz}) = StructTypes.Mutable()
julia> a =Baz(Dict{Foo, Bar}(Foo("a") =>Bar("b"), Foo("c") =>Bar("d")))
Baz(Dict{Foo,Bar}(Foo("a") =>Bar("b"),Foo("c") =>Bar("d")))
julia>keys(a.dict)
KeySet for a Dict{Foo,Bar} with 2 entries. Keys:Foo("a")
Foo("c")
julia>values(a.dict)
ValueIterator for a Dict{Foo,Bar} with 2 entries. Values:Bar("b")
Bar("d")
julia> b = JSON3.write(a)
"{\"dict\":{\"Foo(\\\"a\\\")\":{\"value\":\"b\"},\"Foo(\\\"c\\\")\":{\"value\":\"d\"}}}"
julia> c = JSON3.read(b, Baz)
Baz(Dict{Foo,Bar}(Foo("Foo(\"c\")") =>Bar("d"),Foo("Foo(\"a\")") =>Bar("b")))
julia>keys(c.dict)
KeySet for a Dict{Foo,Bar} with 2 entries. Keys:Foo("Foo(\"c\")")
Foo("Foo(\"a\")")
julia>values(c.dict)
ValueIterator for a Dict{Foo,Bar} with 2 entries. Values:Bar("d")
Bar("b")
Yeah, this is related to #34; the problem is that JSON requires keys to be strings, so we always write/parse keys as strings. Your example might be more complex, but perhaps you want Foo to actually be StructTypes.StringType? That is currently supported and works fine (writing/reading), but yeah, still need to think more broadly about whether we can find a way to support non-StructTypes.StringType types as keys in DictType writing/reading.
Hmm. This might be tricky. In a different example, I will have a Dict where the keys are of type Foo where Foo might be something like this:
struct Foo
a::String
b::Int
c::SomeOtherType
So it will be tricky to get Foo to be of type StructTypes.StringType.
One workaround I've found is to convert the Dict to a vector of key-value pairs before writing. Then when you read it back in, you convert the vector of pairs back to a Dict. It's not very elegant though.
See the MWE below. I would have expected that when I read the object back in, the keys would have been
Foo("a")
andFoo("c")
. Instead, they areFoo("Foo(\"a\")")
andFoo("Foo(\"c\")")
, which is not correct.MWE:
cc: @quinnj
The text was updated successfully, but these errors were encountered: