Yog.IO.JSON (YogEx v0.70.0)

Copy Markdown View Source

JSON format import/export for graph data exchange.

This module provides comprehensive JSON serialization capabilities for graph data, supporting multiple formats used by popular visualization libraries, as well as import functionality for round-trip serialization.

Format Support

  • Generic: Full metadata with type preservation
  • D3Force: D3.js force-directed graphs
  • Cytoscape: Cytoscape.js network visualization
  • VisJs: vis.js network format
  • NetworkX: Python NetworkX compatibility

Examples

Export to JSON

iex> graph = Yog.directed()
...> |> Yog.add_node(1, "Alice")
...> |> Yog.add_node(2, "Bob")
...> |> Yog.add_edge!(from: 1, to: 2, with: "follows")
iex>
iex> json_string = Yog.IO.JSON.to_json(graph, Yog.IO.JSON.default_export_options())
iex> String.contains?(json_string, "Alice")
true

Import from JSON

iex> json = ~s|{"graph_type":"directed","nodes":[{"id":1,"data":"Alice"},{"id":2,"data":"Bob"}],"edges":[{"source":1,"target":2,"weight":"follows"}]}|
iex> {:ok, graph} = Yog.IO.JSON.from_json(json)
iex> Yog.Model.order(graph)
2

Import from Map (PostgreSQL JSONB)

iex> map = %{
...>   "graph_type" => "undirected",
...>   "nodes" => [%{"id" => 1, "data" => "A"}, %{"id" => 2, "data" => "B"}],
...>   "edges" => [%{"source" => 1, "target" => 2, "weight" => 1}]
...> }
iex> {:ok, graph} = Yog.IO.JSON.from_map(map)
iex> Yog.Model.edge_count(graph)
1

Summary

Functions

Creates default export options for String node and edge data.

Converts a JsonError to a string.

Creates export options with custom serializers for generic types.

Parses a JSON string and creates a graph.

Parses a JSON string and creates a graph, raising on error.

Creates a graph from a map (useful for PostgreSQL JSONB).

Quick export for Cytoscape.js with default settings.

Quick export for D3.js force-directed graphs with default settings.

Converts a graph to a JSON string according to options.

Exports a graph to a JSON file.

Exports a multigraph to a JSON file.

Converts a multigraph to a JSON string.

Quick export for vis.js networks with default settings.

Writes a graph to a JSON file using default export options.

Writes a graph to a JSON file with custom export options.

Functions

default_export_options()

Creates default export options for String node and edge data.

error_to_string(error)

Converts a JsonError to a string.

export_options_with(node_serializer, edge_serializer)

Creates export options with custom serializers for generic types.

from_json(json_string)

@spec from_json(String.t()) :: {:ok, Yog.graph()} | {:error, String.t()}

Parses a JSON string and creates a graph.

Supports the generic Yog format and common variations.

Parameters

  • json_string - JSON string to parse

Returns

  • {:ok, graph} - Successfully parsed graph
  • {:error, reason} - Parsing failed

Examples

iex> json = ~s|{"graph_type":"directed","nodes":[{"id":1,"data":"A"}],"edges":[]}|
iex> {:ok, graph} = Yog.IO.JSON.from_json(json)
iex> Yog.Model.order(graph)
1

iex> # NetworkX format
iex> nx_json = ~s|{"directed":true,"multigraph":false,"nodes":[{"id":1}],"links":[]}|
iex> {:ok, graph} = Yog.IO.JSON.from_json(nx_json)
iex> Yog.Model.type(graph)
:directed

iex> # D3 format (nodes + links)
iex> d3_json = ~s|{"nodes":[{"id":1},{"id":2}],"links":[{"source":1,"target":2,"weight":5}]}|
iex> {:ok, graph} = Yog.IO.JSON.from_json(d3_json)
iex> Yog.Model.edge_count(graph)
1

iex> # Cytoscape format
iex> cy_json = ~s|{"elements":[{"data":{"id":1}},{"data":{"id":2}},{"data":{"source":1,"target":2}}]}|
iex> {:ok, graph} = Yog.IO.JSON.from_json(cy_json)
iex> Yog.Model.order(graph)
2

from_json!(json_string)

@spec from_json!(String.t()) :: Yog.graph()

Parses a JSON string and creates a graph, raising on error.

Examples

iex> json = ~s|{"graph_type":"undirected","nodes":[],"edges":[]}|
iex> graph = Yog.IO.JSON.from_json!(json)
iex> Yog.Model.order(graph)
0

from_map(map)

@spec from_map(map()) :: {:ok, Yog.graph()} | {:error, String.t()}

Creates a graph from a map (useful for PostgreSQL JSONB).

Auto-detects format based on keys present in the map.

Parameters

  • map - Map containing graph data

Supported Formats

  • Yog Generic: %{"graph_type" => "directed", "nodes" => [...], "edges" => [...]}
  • NetworkX: %{"directed" => true, "nodes" => [...], "links" => [...]}
  • D3: %{"nodes" => [...], "links" => [...]}
  • Cytoscape: %{"elements" => [...]}
  • VisJs: %{"nodes" => [...], "edges" => [...]} (with from/to)

Examples

iex> map = %{
...>   "graph_type" => "undirected",
...>   "nodes" => [%{"id" => 1, "data" => "Node A"}],
...>   "edges" => []
...> }
iex> {:ok, graph} = Yog.IO.JSON.from_map(map)
iex> Yog.Model.order(graph)
1

iex> # From PostgreSQL JSONB (simple format)
iex> simple = %{
...>   "type" => "directed",
...>   "nodes" => [%{"id" => 1}, %{"id" => 2}],
...>   "edges" => [%{"from" => 1, "to" => 2}]
...> }
iex> {:ok, graph} = Yog.IO.JSON.from_map(simple)
iex> Yog.has_edge?(graph, 1, 2)
true

to_cytoscape_json(graph, node_serializer, edge_serializer)

Quick export for Cytoscape.js with default settings.

to_d3_json(graph, node_serializer, edge_serializer)

Quick export for D3.js force-directed graphs with default settings.

to_json(graph, options)

Converts a graph to a JSON string according to options.

to_json_file(graph, path, options)

Exports a graph to a JSON file.

to_json_file_multi(graph, path, options)

Exports a multigraph to a JSON file.

to_json_multi(graph, options)

Converts a multigraph to a JSON string.

to_visjs_json(graph, node_serializer, edge_serializer)

Quick export for vis.js networks with default settings.

write(path, graph)

Writes a graph to a JSON file using default export options.

write_with(path, options, graph)

Writes a graph to a JSON file with custom export options.