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

Subtype Key not included in JSON3.write #299

Open
BenCurran98 opened this issue Dec 5, 2024 · 0 comments
Open

Subtype Key not included in JSON3.write #299

BenCurran98 opened this issue Dec 5, 2024 · 0 comments

Comments

@BenCurran98
Copy link

Hi, firstly thanks for the package, really cool :)
I've noticed when trying to implement the JSON3 plumbing for parametric types, JSON3.write doesn't include the subtype key in its output if it's not a field name? This means that the round trip of writing and then reading back in some parametric structs won't work as expected (unless in my understanding you make the sub type key a field?) . E.g.

using JSON3
using StructTypes

struct A{T}
    x::T
end

StructTypes.StructType(::Type{A}) = StructTypes.AbstractType()
StructTypes.StructType(::Type{A{T}}) where T = StructTypes.Struct()
StructTypes.subtypekey(::Type{A}) = :_type

# just define this for one type for this example
StructTypes.subtypes(::Type{A}) = NamedTuple{ (Symbol("A{Float64}"),)}( (A{Float64}))

# errors
a = A{Float64}(1.0)
JSON3.read(JSON3.write(a), A)

Since in our use case we'd like to be able to automatically define this plumbing purely based on the parametric type T, it would be great to automatically include the subtype key in the write code rather than expect it to be a field?

Thanks alot!

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

1 participant