JSON3.jl icon indicating copy to clipboard operation
JSON3.jl copied to clipboard

circular dependency - stackoverflow

Open cwiese opened this issue 2 years ago • 0 comments

Circular dependency is so common - this is one example ....StackOverflowError

I wish there was a way to declare "do not nest type" - but to use a ID field ...

using StructTypes,JSON3
abstract type P3Type end
abstract type HierAT <: P3Type end
abstract type DimHierAT <: P3Type end
mutable struct DimHier <: DimHierAT
    id::Int
    dim::String
    hier::Union{HierAT, Nothing}
    levelInd::Int64
end

mutable struct Hier <: HierAT
    id::Int
    hierName::String
    dimHiers::Vector{DimHier} 
end

dh1=DimHier(1, "county", nothing, 1)
dh2=DimHier(2, "state", nothing, 2)
dh3=DimHier(3, "city", nothing, 3)
h1=Hier(1, "Geography", [dh1,dh2,dh3])
dh1.hier=h1
dh2.hier=h1
dh3.hier=h1
StructTypes.StructType(::Type{Hier}) = StructTypes.Struct()
StructTypes.StructType(::Type{DimHier}) = StructTypes.Struct()
JSON3.write(h1)
ERROR: StackOverflowError:
Stacktrace:
     [1] foreachfield
       @ ~/.julia/packages/StructTypes/Cmlkm/src/StructTypes.jl:640 [inlined]
     [2] #write#73
       @ ~/.julia/packages/JSON3/tqMvg/src/write.jl:132 [inlined]
     [3] write

this project does a wonderful job of serializing a data model to JSON :

https://api.emberjs.com/ember-data/release/classes/JSONSerializer

cwiese avatar Dec 02 '21 17:12 cwiese