Skip to content

using CustomStruct lowering for AbstractType #58

@stonepreston

Description

@stonepreston

Hello. I'm very new to julia and am trying to use your package in conjunction with JSON3 to do some serialization of an abstract type. Originally I used JSON.jl but stumbled across this package which has the very cool feature of being able to write json to a Type so I am trying to switch over to JSON3.

With JSON.jl, I could specify a lowering function on an abstract type (in this case I am taking in an abstract type and wrapping it in a simple wrapper type called System:

struct System 
    parameters::Vector{Parameter}
    states::Vector{String}
    equations::Vector{String}
    connections::Vector{String}
end

JSON.lower(system::ModelingToolkit.AbstractSystem) = System(
    build_parameters(system),
    string.(states(system)),
    string.(equations(system)),
    get_connections(system)
)

That works fine and nothing else is needed.

With JSON3 however I found that in addition to registering the abstract type as a custom struct, I also have to register the sub types:

StructTypes.StructType(::Type{System}) = StructTypes.Struct()
StructTypes.StructType(::Type{ModelingToolkit.AbstractSystem}) = StructTypes.CustomStruct()
StructTypes.StructType(::Type{ModelingToolkit.ODESystem}) = StructTypes.CustomStruct()


StructTypes.lower(system::ModelingToolkit.AbstractSystem) = System(
    build_parameters(system),
    string.(states(system)),
    string.(equations(system)),
    get_connections(system)
)

if the system passed in to lower is of type ODESystem (or any other subtype of AbstractSystem) then I have to provide that CustomStruct type mapping for that. Is that normal? Am I doing something wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions