Skip to content

Commit

Permalink
BUILD: Move from JSON.jl to JSON3.jl.
Browse files Browse the repository at this point in the history
  • Loading branch information
epatters committed Dec 13, 2023
1 parent 7287452 commit 54293b5
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 35 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions docs/literate/graphics/graphviz_wiring_diagrams.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
4 changes: 2 additions & 2 deletions src/graphics/GraphvizWiringDiagrams.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"))

Check warning on line 533 in src/graphics/GraphvizWiringDiagrams.jl

View check run for this annotation

Codecov / codecov/patch

src/graphics/GraphvizWiringDiagrams.jl#L533

Added line #L533 was not covered by tests
graphviz_layout(diagram, parse_graphviz(doc))
end

Expand Down
8 changes: 4 additions & 4 deletions src/wiring_diagrams/GraphML.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Check warning on line 195 in src/wiring_diagrams/GraphML.jl

View check run for this annotation

Codecov / codecov/patch

src/wiring_diagrams/GraphML.jl#L194-L195

Added lines #L194 - L195 were not covered by tests

convert_to_graphml_data(x) = convert_to_graph_data(x)

Expand Down Expand Up @@ -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)

Check warning on line 354 in src/wiring_diagrams/GraphML.jl

View check run for this annotation

Codecov / codecov/patch

src/wiring_diagrams/GraphML.jl#L354

Added line #L354 was not covered by tests

convert_from_graphml_data(T::Type, data) = convert_from_graph_data(T, data)

Expand Down
14 changes: 10 additions & 4 deletions src/wiring_diagrams/JSON.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)

Check warning on line 39 in src/wiring_diagrams/JSON.jl

View check run for this annotation

Codecov / codecov/patch

src/wiring_diagrams/JSON.jl#L39

Added line #L39 was not covered by tests
else
JSON3.pretty(io, data, JSON3.AlignmentContext(indent=indent))
end
end
end

Expand Down Expand Up @@ -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))

Check warning on line 124 in src/wiring_diagrams/JSON.jl

View check run for this annotation

Codecov / codecov/patch

src/wiring_diagrams/JSON.jl#L124

Added line #L124 was not covered by tests
end
function parse_json_graph(
::Type{BoxValue}, ::Type{PortValue}, ::Type{WireValue},
Expand Down
2 changes: 1 addition & 1 deletion test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions test/graphics/GraphvizGraphs.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module TestGraphvizGraphs
using Test
import JSON
import JSON3

using Catlab.Theories, Catlab.Graphs, Catlab.Graphics.GraphvizGraphs
import Catlab.Graphics: Graphviz
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/graphics/GraphvizWiringDiagrams.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module TestGraphvizWiringDiagrams

using Test
import JSON
import JSON3

using Catlab.Theories, Catlab.WiringDiagrams, Catlab.Graphics
using Catlab.CategoricalAlgebra: @acset
Expand Down Expand Up @@ -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)
Expand Down
27 changes: 13 additions & 14 deletions test/wiring_diagrams/JSON.jl
Original file line number Diff line number Diff line change
@@ -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]))
Expand Down

0 comments on commit 54293b5

Please sign in to comment.