Serialization

Format

Node

A node is serialized as an object containing the following keys:

  • key any The node's key,
  • attributes [object] The node's attributes (can be omitted or null).
graph.addNode('Thomas', {age: 34});
graph.exportNode('Thomas');
>>> {key: 'Thomas', attributes: {age: 34}}

Edge

An edge is serialized as an object containing the following keys:

  • key [any] The edge's key (can be omitted or null on import),
  • source any The edge's source,
  • target any The edge's target,
  • attributes [object] The edge's attributes (can be omitted or null),
  • undirected [boolean] Whether the edge is undirected (can be omitted or null).
graph.addNodesFrom(['Thomas', 'Eric']);
graph.addEdgeWithKey('T->E', 'Thomas', 'Eric', {type: 'KNOWS'});
graph.exportEdge('T->E');
>>> {
  key: 'T->E',
  source: 'Thomas',
  target: 'Eric',
  attributes: {type: 'KNOWS'},
  undirected: false
}

Graph

A graph is serializd as an object containing an attributes, a nodes & an edges key:

  • object attributes: containing the attributes of the graph (can be omitted).
  • object nodes: containing a list of serialized nodes (can be omitted when merging).
  • object edges: containing a list of serialized edges (can be omitted).
graph.addNodesFrom(['Thomas', 'Eric']);
graph.addEdgeWithKey('T->E', 'Thomas', 'Eric', {type: 'KNOWS'});
graph.setAttribute('name', 'My Graph');
graph.export();
>>> {
  attributes: {
    name: 'My Graph'
  },
  nodes: [
    {key: 'Thomas'},
    {key: 'Eric'}
  ],
  edges: [
    {
      key: 'T->E',
      source: 'Thomas',
      target: 'Eric',
      attributes: {type: 'KNOWS'}
    }
  ]
}

#.import

Imports a whole serialized graph into the graph.

Example

graph.import({
  attributes: {name: 'My Graph'},
  nodes: [{key: 'Thomas'}, {key: 'Eric'}],
  edges: [{source: 'Thomas', target: 'Eric'}]
});

graph.hasNode('Thomas');
>>> true

Arguments

  • data serialized graph|Graph: serialized graph data or another Graph instance.
  • merge [boolean] false: whether to merge the imported data.

#.importNode

Imports a single serialized node into the graph.

Example

graph.importNode({key: 'Thomas', attributes: {eyes: 'blue'}});

graph.getNodeAttribute('Thomas', 'eyes');
>>> 'blue'

Arguments

  • node serialized node: data of the node to import.
  • merge [boolean] false: whether to merge the imported node.

#.importNodes

Imports a list of serialized nodes into the graph.

Example

graph.importNodes([
  {key: 'Thomas', attributes: {age: 7}},
  {key: 'Eric', attributes: {age: 45}}
]);

graph.getNodeAttribute('Eric', 'age');
>>> 45

Arguments

  • nodes array: array of serialized nodes.
  • merge [boolean] false: whether to merge the imported nodes.

#.importEdge

Imports a single serialized edge into the graph.

Example

graph.addNodesFrom(['Thomas', 'Eric']);
graph.importEdge({
  key:'T->E',
  source: 'Thomas',
  target: 'Eric',
  attributes: {type: 'KNOWS'}
});

graph.hasEdge('Thomas', 'Eric');
>>> true

Arguments

  • edge serialized edge: data of the edge to import.
  • merge [boolean] false: whether to merge the imported edge.

#.importEdges

Imports a list of serialized edges into the graph.

Example

graph.addNodesFrom(['Thomas', 'Eric', 'John']);
graph.importEdges([
  {
    key:'T->E',
    source: 'Thomas',
    target: 'Eric',
    attributes: {type: 'KNOWS'}
  },
  {
    key:'T->J',
    source: 'Thomas',
    target: 'John',
    attributes: {type: 'KNOWS'}
  }
]);

graph.edges('Thomas');
>>> ['T->E', 'T->J']

Arguments

  • edges array: array of serialized edges.
  • merge [boolean] false: whether to merge the imported edges.

#.export

Exports the whole instance's data as a serialized graph.

Example

graph.addNodesFrom(['Thomas', 'Eric']);
graph.addEdgeWithKey('T->E', 'Thomas', 'Eric', {type: 'KNOWS'});
graph.setAttribute('name', 'My Graph');
graph.export();
>>> {
  attributes: {
    name: 'My Graph'
  },
  nodes: [
    {key: 'Thomas'},
    {key: 'Eric'}
  ],
  edges: [
    {
      key: 'T->E',
      source: 'Thomas',
      target: 'Eric',
      attributes: {type: 'KNOWS'}
    }
  ]
}

#.exportNode

Exports a single node from the graph.

Example

graph.addNode('Thomas', {age: 34});

graph.exportNode('Thomas');
>>> {key: 'Thomas', attributes: {age: 34}}

#.exportNodes

Exports every node or only a bunch of nodes from the graph.

Example

graph.addNodesFrom({
  Thomas: {age: 34},
  Martha: {age: 26}
});

graph.exportNodes();
>>> [
  {key: 'Thomas', attributes: {age: 34}},
  {key: 'Eric', attributes: {age: 26}}
]

graph.exportNodes(['Thomas']);
>>> [
  {key: 'Thomas', attributes: {age: 34}},
]

Arguments

  1. None: exporting every node.
  2. Bunch of nodes: exporting the given bunch.
    • nodes [bunch]: bunch of nodes to export.

#.exportEdge

Exports a single edge from the graph.

Example

graph.addNodesFrom(['Thomas', 'Martha']);
graph.addEdgeWithKey('T->M', 'Thomas', 'Martha', {type: 'KNOWS'});

graph.exportEdge('T->M');
>>> {
  key: 'T->M',
  source: 'Thomas',
  target: 'Martha',
  attributes: {type: 'KNOWS'}
}

#.exportEdges

Exports every edge or only a bunch of edges from the graph.

Example

graph.addNodesFrom(['Thomas', 'Martha', 'Eric']);
graph.addEdgeWithKey('T->M', 'Thomas', 'Martha', {type: 'KNOWS'});
graph.addEdgeWithKey('M->E', 'Martha', 'Eric', {type: 'KNOWS'});

graph.exportEdges();
>>> [
  {
    key: 'T->M',
    source: 'Thomas',
    target: 'Martha',
    attributes: {type: 'KNOWS'}
  },
  {
    key: 'M->E',
    source: 'Martha',
    target: 'Eric'
  }
]

graph.exportEdges(['T->M']);
>>> [
  {
    key: 'T->M',
    source: 'Thomas',
    target: 'Martha',
    attributes: {type: 'KNOWS'}
  }
]

Arguments

  1. None: exporting every edge.
  2. Bunch of edges: exporting the given bunch.
    • edges [bunch]: bunch of edges to export.

#.exportDirectedEdges

Exports every directed edge or only the directed edges in the provided bunch from the graph.

Example

graph.addNodesFrom(['Thomas', 'Martha', 'Eric']);
graph.addEdgeWithKey('T->M', 'Thomas', 'Martha', {type: 'KNOWS'});
graph.addEdgeWithKey('M->E', 'Martha', 'Eric', {type: 'KNOWS'});
graph.addUndirectedEdgeWithKey('A', 'Thomas', 'Eric');

graph.exportDirectedEdges();
>>> [
  {
    key: 'T->M',
    source: 'Thomas',
    target: 'Martha',
    attributes: {type: 'KNOWS'}
  },
  {
    key: 'M->E',
    source: 'Martha',
    target: 'Eric'
  }
]

// Undirected edges are filtered
graph.exportDirectedEdges(['T->M', 'A']);
>>> [
  {
    key: 'T->M',
    source: 'Thomas',
    target: 'Martha',
    attributes: {type: 'KNOWS'}
  }
]

Arguments

  1. None: exporting every edge.
  2. Bunch of edges: exporting the given bunch.
    • edges [bunch]: bunch of edges to export (undirected will be filtered).

#.exportUndirectedEdges

Exports every undirected edge or only the undirected edges in the provided bunch from the graph.

Example

graph.addNodesFrom(['Thomas', 'Martha', 'Eric']);
graph.addEdgeWithKey('T->M', 'Thomas', 'Martha', {type: 'KNOWS'});
graph.addEdgeWithKey('M->E', 'Martha', 'Eric', {type: 'KNOWS'});
graph.addUndirectedEdgeWithKey('A', 'Thomas', 'Eric');

graph.exportUndirectedEdges();
>>> [
  {
    key: 'A',
    source: 'Thomas',
    target: 'Eric',
    undirected: true
  }
]

// Directed edges are filtered
graph.exportUndirectedEdges(['A', 'T->M']);
>>> [
  {
    key: 'A',
    source: 'Thomas',
    target: 'Eric',
    undirected: true
  }
]

Arguments

  1. None: exporting every edge.
  2. Bunch of edges: exporting the given bunch.
    • edges [bunch]: bunch of edges to export (directed will be filtered).