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.

#.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.

#.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}}

#.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'}
}