TaeraeGraph.fromJson constructor

TaeraeGraph.fromJson(
  1. Map<String, Object?> json
)

Rebuilds a graph from JSON data generated by toJson.

Implementation

factory TaeraeGraph.fromJson(Map<String, Object?> json) {
  final TaeraeGraph graph = TaeraeGraph();
  final Object? rawNodes = json['nodes'];
  final Object? rawEdges = json['edges'];

  if (rawNodes != null && rawNodes is! List<Object?>) {
    throw FormatException('Expected "nodes" to be a list.');
  }
  if (rawEdges != null && rawEdges is! List<Object?>) {
    throw FormatException('Expected "edges" to be a list.');
  }

  if (rawNodes is List<Object?>) {
    for (int index = 0; index < rawNodes.length; index++) {
      final TaeraeNode node = TaeraeNode.fromJson(
        readStringKeyedMap(rawNodes[index], 'nodes[$index]'),
      );
      graph.upsertNode(
        node.id,
        labels: node.labels,
        properties: node.properties,
      );
    }
  }

  if (rawEdges is List<Object?>) {
    for (int index = 0; index < rawEdges.length; index++) {
      final TaeraeEdge edge = TaeraeEdge.fromJson(
        readStringKeyedMap(rawEdges[index], 'edges[$index]'),
      );
      graph.upsertEdge(
        edge.id,
        edge.from,
        edge.to,
        type: edge.type,
        properties: edge.properties,
      );
    }
  }

  return graph;
}