Version: dev

to_json_graph

Stable function · Graphs

Tier: stable · Kind: function · Topic: Graphs

JuliaTypeScriptPythonRustGo
to_json_graphtoJsonGraphto_json_graphto_json_graphToJSONGraph

Julia

to_json_graph(graph::Graph{ComponentNode, CouplingEdge})
to_json_graph(graph::Graph{VariableNode, DependencyEdge})
to_json_graph(graph) -> String

Export graph to the JSON adjacency list of esm-libraries-spec §4.8.3.

Three top-level keys:

  • nodes — each node’s own fields, prefixed with the id that keys it (a component node’s id, a variable node’s name);
  • edges — {"source", "target", "data"}, endpoints by node key with the edge payload under data;
  • adjacency — every node key mapped to its UNDIRECTED neighbours, matching adjacency .

Pinned by tests/conformance/graph/cases.json at the level of the top-level keys, the node ids, the edge endpoints and the adjacency map; the per-node and per-edge payloads are this binding’s own and are not.

The canonical name across the bindings is to_json_graph (API_SPEC.md §8 item 8) — the spelling TypeScript (toJsonGraph), Python and Rust already use — because §8 item 2 gave to_json to the DOCUMENT serializer, and one binding cannot make one name mean both without dispatch a caller can only discover by reading the source. Julia’s to_json(::Graph) methods stay one minor as a deprecated alias (§10) and render byte-identical output.

Source

TypeScript

function toJsonGraph<N extends object, E>(graph: Graph<N, E>): string

Export graph as JSON adjacency list format for web consumption.

Source

Python

to_json_graph(graph: Graph[Any, Any]) -> str

Render graph as the JSON adjacency list of esm-libraries-spec §4.8.3.

Three top-level keys:

  • nodes — each node’s own fields, prefixed with the id that keys it (a component node’s id, a variable node’s name);
  • edges — {"source", "target", "data"}, endpoints by node key with the edge payload under data;
  • adjacency — every node key mapped to its UNDIRECTED neighbours, which is what Graph.adjacency returns.

Pinned by tests/conformance/graph/cases.json at the level of the top-level keys, the node ids, the edge endpoints and the adjacency map; the per-node and per-edge payloads are this binding’s own and are not.

Source

Rust

pub fn to_json_graph<G: Graph>(graph: &G) -> String

Render graph as a JSON adjacency-list document (esm-libraries-spec §4.8.3). Named to_json_graph, not to_json, because to_json is the DOCUMENT serializer (API_SPEC.md §8 item 2). See to_dot .

Source · Rust docs

Example (expression_graph)

let doc = load_path(fixture("decay.esm"))?;
let model = &doc.models.as_ref().unwrap()["Decay"];
let graph = expression_graph(model);
let json = to_json_graph(&graph);
println!("{json}");
assert!(json.contains("\"lambda\"") && json.contains("\"N\""));

From docs/api/examples/rust/graphs.rs

Go

func ToJSONGraph(g Graph) (string, error)

ToJSONGraph renders a graph as a JSON adjacency list.

Canonical to_json_graph (API_SPEC.md §8 item 8) — NOT ToJSON, which is the DOCUMENT serializer settled by §8 item 2 and cannot carry both meanings. It accepts a *ComponentGraph or an *ExpressionGraph.

Source · Go docs