diff --git a/Project.toml b/Project.toml index 29f243580..d550cbed3 100644 --- a/Project.toml +++ b/Project.toml @@ -13,7 +13,7 @@ Compose = "a81c6b42-2e10-5240-aca2-a61377ecd94b" DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" GATlab = "f0ffcf3b-d13a-433e-917c-cc44ccf5ead2" GeneralizedGenerated = "6b9d7cbe-bcb9-11e9-073f-15a7a543e2eb" -JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" +JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1" LightXML = "9c8b4983-aa76-5018-a973-4c85ecc9e179" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" Logging = "56ddb016-857b-54e1-b83d-db4d58db5568" @@ -58,7 +58,7 @@ GATlab = "0.1" GeneralizedGenerated = "0.2, 0.3" Graphs = "1" Graphviz_jll = "2" -JSON = "0.20, 0.21" +JSON3 = "1" LightXML = "0.8, 0.9" LinearAlgebra = "1.9" Logging = "1.9" diff --git a/docs/Project.toml b/docs/Project.toml index c6bedb1e7..d5094855d 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -6,7 +6,7 @@ Convex = "f65535da-76fb-5f13-bab9-19810c17039a" DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" GATlab = "f0ffcf3b-d13a-433e-917c-cc44ccf5ead2" -JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" +JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1" JSONSchema = "7d188eb4-7ad8-530c-ae41-71a32a6d4692" LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f" Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306" diff --git a/docs/literate/graphics/graphviz_wiring_diagrams.jl b/docs/literate/graphics/graphviz_wiring_diagrams.jl index 6801db77a..5a37867b4 100644 --- a/docs/literate/graphics/graphviz_wiring_diagrams.jl +++ b/docs/literate/graphics/graphviz_wiring_diagrams.jl @@ -170,6 +170,6 @@ Graphviz.pprint(graph) # Catlab provides a simple wrapper around the Graphviz command-line programs. # For example, here is the JSON output for the graph. -import JSON +import JSON3 -JSON.parse(Graphviz.run_graphviz(graph, format="json0")) +JSON3.read(Graphviz.run_graphviz(graph, format="json0")) diff --git a/src/graphics/GraphvizWiringDiagrams.jl b/src/graphics/GraphvizWiringDiagrams.jl index d8c868bc3..1cb1fd817 100644 --- a/src/graphics/GraphvizWiringDiagrams.jl +++ b/src/graphics/GraphvizWiringDiagrams.jl @@ -5,7 +5,7 @@ This module requires Graphviz v2.42 or higher. module GraphvizWiringDiagrams export graphviz_layout, to_graphviz, to_graphviz_property_graph -import JSON +import JSON3 using LinearAlgebra: normalize using MLStyle: @match using StaticArrays @@ -530,7 +530,7 @@ the wires are ignored. """ function graphviz_layout(diagram::WiringDiagram; kw...) graph = to_graphviz(diagram; kw...) - doc = JSON.parse(Graphviz.run_graphviz(graph, format="json0")) + doc = JSON3.read(Graphviz.run_graphviz(graph, format="json0")) graphviz_layout(diagram, parse_graphviz(doc)) end diff --git a/src/wiring_diagrams/GraphML.jl b/src/wiring_diagrams/GraphML.jl index 513943e28..2f862b0b4 100644 --- a/src/wiring_diagrams/GraphML.jl +++ b/src/wiring_diagrams/GraphML.jl @@ -18,7 +18,7 @@ export read_graphml, parse_graphml, write_graphml, generate_graphml, convert_from_graphml_data, convert_to_graphml_data using DataStructures: OrderedDict -import JSON +import JSON3 using LightXML using ...Graphs @@ -191,8 +191,8 @@ generate_graphml_data_type(::Type{Vector{T}}) where T = "json" generate_graphml_data_value(x::Number) = string(x) generate_graphml_data_value(x::String) = x generate_graphml_data_value(x::Symbol) = string(x) -generate_graphml_data_value(x::Dict) = JSON.json(x) -generate_graphml_data_value(x::Vector) = JSON.json(x) +generate_graphml_data_value(x::Dict) = JSON3.write(x) +generate_graphml_data_value(x::Vector) = JSON3.write(x) convert_to_graphml_data(x) = convert_to_graph_data(x) @@ -351,7 +351,7 @@ parse_graphml_data_value(::Type{Val{:long}}, s::String) = parse(Int, s) parse_graphml_data_value(::Type{Val{:float}}, s::String) = parse(Float32, s) parse_graphml_data_value(::Type{Val{:double}}, s::String) = parse(Float64, s) parse_graphml_data_value(::Type{Val{:string}}, s::String) = s -parse_graphml_data_value(::Type{Val{:json}}, s::String) = JSON.parse(s) +parse_graphml_data_value(::Type{Val{:json}}, s::String) = JSON3.read(s) convert_from_graphml_data(T::Type, data) = convert_from_graph_data(T, data) diff --git a/src/wiring_diagrams/JSON.jl b/src/wiring_diagrams/JSON.jl index f8d22937b..a5deece29 100644 --- a/src/wiring_diagrams/JSON.jl +++ b/src/wiring_diagrams/JSON.jl @@ -19,7 +19,7 @@ export read_json_graph, parse_json_graph, write_json_graph, generate_json_graph, convert_from_json_graph_data, convert_to_json_graph_data using DataStructures: OrderedDict -import JSON +import JSON3 using ..DirectedWiringDiagrams, ..WiringDiagramSerialization @@ -33,8 +33,13 @@ const PortData = NamedTuple{(:kind,:port),Tuple{PortKind,Int}} """ function write_json_graph(diagram::WiringDiagram, filename::String; indent::Union{Int,Nothing}=nothing) + data = generate_json_graph(diagram) open(filename, "w") do io - JSON.print(io, generate_json_graph(diagram), indent) + if isnothing(indent) + JSON3.write(io, data) + else + JSON3.pretty(io, data, JSON3.AlignmentContext(indent=indent)) + end end end @@ -108,14 +113,15 @@ convert_to_json_graph_data(x) = convert_to_graph_data(x) """ function read_json_graph( BoxValue::Type, PortValue::Type, WireValue::Type, filename::String) - parse_json_graph(BoxValue, PortValue, WireValue, JSON.parsefile(filename)) + json_string = read(filename, String) + parse_json_graph(BoxValue, PortValue, WireValue, JSON3.read(json_string)) end """ Parse a wiring diagram from a JSON string or dict. """ function parse_json_graph( BoxValue::Type, PortValue::Type, WireValue::Type, s::Union{AbstractString,IO}) - parse_json_graph(BoxValue, PortValue, WireValue, JSON.parse(s)) + parse_json_graph(BoxValue, PortValue, WireValue, JSON3.read(s)) end function parse_json_graph( ::Type{BoxValue}, ::Type{PortValue}, ::Type{WireValue}, diff --git a/test/Project.toml b/test/Project.toml index 0a1303586..fd0223c50 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -8,7 +8,7 @@ DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" GATlab = "f0ffcf3b-d13a-433e-917c-cc44ccf5ead2" Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6" IterativeSolvers = "42fd0dbc-a981-5370-80f2-aaf504508153" -JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" +JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1" LightXML = "9c8b4983-aa76-5018-a973-4c85ecc9e179" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" MetaGraphs = "626554b9-1ddb-594c-aa3c-2596fe9399a5" diff --git a/test/graphics/GraphvizGraphs.jl b/test/graphics/GraphvizGraphs.jl index 50b3f4c44..109668193 100644 --- a/test/graphics/GraphvizGraphs.jl +++ b/test/graphics/GraphvizGraphs.jl @@ -1,6 +1,6 @@ module TestGraphvizGraphs using Test -import JSON +import JSON3 using Catlab.Theories, Catlab.Graphs, Catlab.Graphics.GraphvizGraphs import Catlab.Graphics: Graphviz @@ -19,14 +19,14 @@ const stmts = Graphviz.filter_statements data_path(name::String) = joinpath(@__DIR__, "data", name) # Undirected simple graph. -doc = open(JSON.parse, data_path("graphviz_graph.json"), "r") +doc = open(JSON3.read, data_path("graphviz_graph.json"), "r") parsed = parse_graphviz(doc) @test parsed isa SymmetricPropertyGraph @test nv(parsed) == 10 @test ne(parsed) รท 2 == 13 # Directed simple graph -doc = open(JSON.parse, data_path("graphviz_digraph.json"), "r") +doc = open(JSON3.read, data_path("graphviz_digraph.json"), "r") parsed = parse_graphviz(doc) @test parsed isa PropertyGraph @test nv(parsed) == 5 diff --git a/test/graphics/GraphvizWiringDiagrams.jl b/test/graphics/GraphvizWiringDiagrams.jl index 996bb8665..d224f7e29 100644 --- a/test/graphics/GraphvizWiringDiagrams.jl +++ b/test/graphics/GraphvizWiringDiagrams.jl @@ -1,7 +1,7 @@ module TestGraphvizWiringDiagrams using Test -import JSON +import JSON3 using Catlab.Theories, Catlab.WiringDiagrams, Catlab.Graphics using Catlab.CategoricalAlgebra: @acset @@ -101,7 +101,7 @@ graph = to_graphviz(d, box_labels=true, junction_labels=true, port_labels=true) ################# diagram = include(joinpath("data", "graphviz_wiring_diagram.jl")) -doc = open(JSON.parse, +doc = open(JSON3.read, joinpath(@__DIR__, "data", "graphviz_wiring_diagram.json"), "r") graph = parse_graphviz(doc) layout = graphviz_layout(diagram, graph) diff --git a/test/wiring_diagrams/JSON.jl b/test/wiring_diagrams/JSON.jl index 98925b6d4..46eea11a8 100644 --- a/test/wiring_diagrams/JSON.jl +++ b/test/wiring_diagrams/JSON.jl @@ -1,34 +1,33 @@ module TestJSONWiringDiagrams using Test -import JSON using Catlab.Theories, Catlab.WiringDiagrams -# Round trip wiring diagrams with dictionary box and wire data. - -function roundtrip(f::WiringDiagram) - json = sprint(JSON.print, generate_json_graph(f), 2) - parse_json_graph(Dict, Nothing, Dict, JSON.parse(json)) +function roundtrip(BoxT, PortT, WireT, f::WiringDiagram) + mktempdir() do dir + path = joinpath(dir, "dwd.json") + write_json_graph(f, path, indent=2) + read_json_graph(BoxT, PortT, WireT, path) + end end +# Round trip wiring diagrams with dictionary box and wire data. + ports(n) = Nothing[ nothing for i in 1:n ] diagram = WiringDiagram(ports(1), ports(1)) -f = Box(Dict("name" => "f", "type" => "foo"), ports(1), ports(1)) +f = Box(Dict(:name => "f", :type => "foo"), ports(1), ports(1)) fv = add_box!(diagram, f) add_wires!(diagram, [ - Wire(Dict("name" => "w1", "type" => "woo"), + Wire(Dict(:name => "w1", :type => "woo"), (input_id(diagram), 1) => (fv, 1)), - Wire(Dict("name" => "w2", "type" => "woo"), + Wire(Dict(:name => "w2", :type => "woo"), (fv, 1) => (output_id(diagram), 1)), ]) -@test roundtrip(diagram) == diagram +@test roundtrip(Dict, Nothing, Dict, diagram) == diagram # Round trip wiring diagrams with symbolic box and port values. -function roundtrip_symbolic(f::WiringDiagram) - json = sprint(JSON.print, generate_json_graph(f), 2) - parse_json_graph(Symbol, Symbol, Nothing, JSON.parse(json)) -end +roundtrip_symbolic(f::WiringDiagram) = roundtrip(Symbol, Symbol, Nothing, f) f = singleton_diagram(Box(:f, [:A], [:B])) g = singleton_diagram(Box(:g, [:B], [:C]))