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

Why are keys of a Dict not unpacked before serialization? #286

Closed
simsurace opened this issue Aug 7, 2024 · 3 comments
Closed

Why are keys of a Dict not unpacked before serialization? #286

simsurace opened this issue Aug 7, 2024 · 3 comments

Comments

@simsurace
Copy link

Is this intentional? Values of a dict are unpacked before serialization, but keys are not:

using JSON3, StructTypes

struct IntWrapper
    val::Int
end

StructTypes.StructType(::Type{IntWrapper}) = StructTypes.NumberType()
StructTypes.numbertype(::Type{IntWrapper}) = Int
Base.Int(id::IntWrapper) = id.val

JSON3.write(IntWrapper(42)) == "42" # true
JSON3.write(Dict(IntWrapper(42) => 42)) == "{\"42\":42}" # false
JSON3.write(Dict(42 => IntWrapper(42))) == "{\"42\":42}" # true

Am I missing something or is this a bug?

@Naunet
Copy link
Contributor

Naunet commented Aug 19, 2024

JSON only allows string keys.

It would appear that the dictionary keys are indeed being unwrapped since the result is a string and not a structure. However they then get "stringified" to be acceptable keys.

Some related discussions that may help you find a work-around:

@quinnj
Copy link
Owner

quinnj commented Aug 19, 2024

Yeah, we have to ensure we produce valid JSON when writing

@simsurace
Copy link
Author

Thanks, this makes sense!

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

3 participants