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

StructTypes.Mutable() does not call the inner constructor #266

Open
BlondDon opened this issue Jul 25, 2023 · 0 comments
Open

StructTypes.Mutable() does not call the inner constructor #266

BlondDon opened this issue Jul 25, 2023 · 0 comments

Comments

@BlondDon
Copy link

BlondDon commented Jul 25, 2023

using JSON3
using StructTypes

Base.@kwdef mutable struct MyType2
    c::Float64 = 3.0

    MyType2(c) = c < 0.0 ? error("bar") : new(c)
end

Base.@kwdef mutable struct MyType
    a::Int = 1
    b::MyType2 = MyType2(3.0)

    MyType(a,b) = a > 10 ? error("foo") : new(a,b)
end

StructTypes.StructType(::Type{MyType}) = StructTypes.Mutable()
StructTypes.StructType(::Type{MyType2}) = StructTypes.Mutable()

json_string = """{"a":11, "b":{"c":-3.0}}"""
hello_world = JSON3.read(json_string, MyType; parsequoted= true)

Dear JSON3.jl maintainers,

Lets assume i have a two mutable structs of MyType2 which is then nested into MyType.
I expect JSON3.read to trow an error since json_string has to invalid input according to inner constructor. My understand is that StuctTypes.Muable() calls an emtpy constructor and then mutates the arguements in order to deal with missing arguments in the json_string. I want it to be able top handly missing arguments but still call the innerconstructor to validate the input.
Is there a way to throw theses errors using StructTypes while parsing the “json_string”.
Due to legacy reasons i don’t want to change the structs to be immutable.
Tips are very appreciated, thank you for your support in advance.

best regards
BlondDon

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