JuliaIO / JSON.jl

JSON parsing and printing
Other
313 stars 101 forks source link

Detect circular dependencies while printing JSON #354

Closed vustef closed 1 year ago

vustef commented 1 year ago

Here's a repro:

using JSON
abstract type AbstractBar end

mutable struct Foo4
    bar::Union{Nothing, AbstractBar}
end

struct Bar4 <: AbstractBar
    foo::Ref{Foo4}
end

foo4 = Foo4(nothing)
bar4 = Bar4(foo4)
foo4.bar = bar4

dict = Dict{String, Any}()
dict["FOO"] = foo4

# Prints forever.
# JSON.print(stdout, dict)

On the other hand, if I just use @show, it prevents infinite recursion:

julia> @show dict
dict = Dict{String, Any}("FOO" => Foo4(Bar4(Base.RefValue{Foo4}(Foo4(#= circular reference @-3 =#)))))
Dict{String, Any} with 1 entry:
  "FOO" => Foo4(Bar4(RefValue{Foo4}(Foo4(#= circular reference @-3 =#))))
vustef commented 1 year ago

Actually, this might have already been fixed with the latest commit: https://github.com/JuliaIO/JSON.jl/commit/2219019b6dfb19c7dcc13754ab65d86225d01146. I'll reopen if not.