Json Module
JSON graph serialization.
Provides functions to export graphs in JSON format for data interchange and web applications.
Example
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: |
|
namespace Yog
namespace Yog.IO
module Model
from Yog
<summary> Core graph data structures and basic operations for the yog library. This module defines the fundamental `Graph` type and provides all basic operations for creating and manipulating graphs. The graph uses an adjacency list representation with dual indexing (both outgoing and incoming edges) for efficient traversal in both directions. </summary>
<summary> Core graph data structures and basic operations for the yog library. This module defines the fundamental `Graph` type and provides all basic operations for creating and manipulating graphs. The graph uses an adjacency list representation with dual indexing (both outgoing and incoming edges) for efficient traversal in both directions. </summary>
val graph: Graph<string,int>
val empty: graphType: GraphType -> Graph<'n,'e>
<summary> Creates a new empty graph of the specified type. ## Example let graph = Model.empty Directed ## Parameters - `graphType`: The type of the graph. ## Returns A new empty graph. </summary>
<summary> Creates a new empty graph of the specified type. ## Example let graph = Model.empty Directed ## Parameters - `graphType`: The type of the graph. ## Returns A new empty graph. </summary>
union case GraphType.Directed: GraphType
<summary> A directed graph where edges have a direction from source to destination. </summary>
<summary> A directed graph where edges have a direction from source to destination. </summary>
val addNode: id: NodeId -> data: 'n -> graph: Graph<'n,'e> -> Graph<'n,'e>
<summary> Adds a node to the graph with the given ID and data. If a node with this ID already exists, its data will be replaced. ## Example graph |> addNode 1 "Node A" |> addNode 2 "Node B" ## Parameters - `id`: The unique identifier for the node. - `data`: The data to store at the node. - `graph`: The graph to add the node to. ## Returns A new graph with the node added. </summary>
<summary> Adds a node to the graph with the given ID and data. If a node with this ID already exists, its data will be replaced. ## Example graph |> addNode 1 "Node A" |> addNode 2 "Node B" ## Parameters - `id`: The unique identifier for the node. - `data`: The data to store at the node. - `graph`: The graph to add the node to. ## Returns A new graph with the node added. </summary>
val addEdge: src: NodeId -> dst: NodeId -> weight: 'e -> graph: Graph<'n,'e> -> Graph<'n,'e>
<summary> Adds an edge to the graph with the given weight. For directed graphs, adds a single edge from src to dst. For undirected graphs, adds edges in both directions. ## Example graph |> addEdge 1 2 10 **Note:** If `src` or `dst` have not been added via `addNode`, an `ArgumentException` is thrown. To automatically create missing endpoints when adding an edge, use `addEdgeEnsured` or `addEdgeEnsuredWith`. ## Parameters - `src`: The source node ID. - `dst`: The destination node ID. - `weight`: The weight of the edge. - `graph`: The graph to add the edge to. ## Returns A new graph with the edge added. </summary>
<summary> Adds an edge to the graph with the given weight. For directed graphs, adds a single edge from src to dst. For undirected graphs, adds edges in both directions. ## Example graph |> addEdge 1 2 10 **Note:** If `src` or `dst` have not been added via `addNode`, an `ArgumentException` is thrown. To automatically create missing endpoints when adding an edge, use `addEdgeEnsured` or `addEdgeEnsuredWith`. ## Parameters - `src`: The source node ID. - `dst`: The destination node ID. - `weight`: The weight of the edge. - `graph`: The graph to add the edge to. ## Returns A new graph with the edge added. </summary>
val json: string
module Json
from Yog.IO
<summary> JSON graph serialization. Provides functions to export graphs in JSON format for data interchange and web applications. ## Example open Yog.IO open Yog.Model let graph = empty Directed |> addNode 1 "Start" |> addNode 2 "End" |> addEdge 1 2 5 // Export to JSON let json = Json.render graph File.WriteAllText("graph.json", json) </summary>
<summary> JSON graph serialization. Provides functions to export graphs in JSON format for data interchange and web applications. ## Example open Yog.IO open Yog.Model let graph = empty Directed |> addNode 1 "Start" |> addNode 2 "End" |> addEdge 1 2 5 // Export to JSON let json = Json.render graph File.WriteAllText("graph.json", json) </summary>
val render: graph: Graph<'n,'e> -> string
<summary> Converts a graph to JSON format. The JSON format is suitable for data interchange, web APIs, and storage. Node and edge data are converted to strings using their `ToString()` method. **Time Complexity:** O(V + E) ## Example let graph = empty Directed |> addNode 1 "A" |> addNode 2 "B" |> addEdge 1 2 10 let json = Json.render graph // { // "kind": "Directed", // "nodes": [ // { "id": 1, "data": "A" }, // { "id": 2, "data": "B" } // ], // "edges": [ // { "source": 1, "target": 2, "weight": "10" } // ] // } ## Use Cases - Web API responses - Data persistence - Interoperability with other languages/tools - Graph database import/export </summary>
<summary> Converts a graph to JSON format. The JSON format is suitable for data interchange, web APIs, and storage. Node and edge data are converted to strings using their `ToString()` method. **Time Complexity:** O(V + E) ## Example let graph = empty Directed |> addNode 1 "A" |> addNode 2 "B" |> addEdge 1 2 10 let json = Json.render graph // { // "kind": "Directed", // "nodes": [ // { "id": 1, "data": "A" }, // { "id": 2, "data": "B" } // ], // "edges": [ // { "source": 1, "target": 2, "weight": "10" } // ] // } ## Use Cases - Web API responses - Data persistence - Interoperability with other languages/tools - Graph database import/export </summary>
Functions and values
| Function or value |
Description
|
||
|
Converts a graph to JSON format. The JSON format is suitable for data interchange, web APIs, and
storage. Node and edge data are converted to strings using their
Time Complexity: O(V + E)
Example
Use Cases
val graph: obj
val json: obj
|
||
|
Renders a graph to a JSON file.
Example
val graph: obj
|