yog/io/json
Types
Values
pub fn default_json_options() -> JsonOptions
Creates default JSON options.
Nodes are { "id": 1, "label": "Node A" }.
Edges are { "source": 1, "target": 2, "weight": "5" }.
pub fn to_json(
graph: model.Graph(String, String),
options: JsonOptions,
) -> String
Converts a graph to a JSON string compatible with many visualization libraries (e.g., D3.js).
The graph’s node data and edge data must be convertible to strings.
Time Complexity: O(V + E)
Example
import gleam/io
import gleam/json
import yog/model
pub fn main() {
let graph =
model.new(model.Directed)
|> model.add_node(1, "Alice")
|> model.add_node(2, "Bob")
|> model.add_edge(from: 1, to: 2, with: "follows")
let json_string = yog_json.to_json(graph, yog_json.default_json_options())
io.println(json_string)
}
This outputs:
{
"nodes": [
{"id": 1, "label": "Alice"},
{"id": 2, "label": "Bob"}
],
"edges": [
{"source": 1, "target": 2, "weight": "follows"}
]
}