to_json_graph
Stable function · Graphs
Tier: stable · Kind: function · Topic: Graphs
| Julia | TypeScript | Python | Rust | Go |
|---|---|---|---|---|
to_json_graph | toJsonGraph | to_json_graph | to_json_graph | ToJSONGraph |
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 theidthat keys it (a component node’sid, a variable node’sname);edges—{"source", "target", "data"}, endpoints by node key with the edge payload underdata;adjacency— every node key mapped to its UNDIRECTED neighbours, matchingadjacency.
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.
TypeScript
function toJsonGraph<N extends object, E>(graph: Graph<N, E>): string
Export graph as JSON adjacency list format for web consumption.
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 theidthat keys it (a component node’sid, a variable node’sname);edges—{"source", "target", "data"}, endpoints by node key with the edge payload underdata;adjacency— every node key mapped to its UNDIRECTED neighbours, which is whatGraph.adjacencyreturns.
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.
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
.
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\""));
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.