Link Search Menu Expand Document

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});
// Serialized would be:
>>> {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.mergeEdgeWithKey('T->E', 'Thomas', 'Eric', {type: 'KNOWS'});
// Serialized would be:
>>> {
  key: 'T->E',
  source: 'Thomas',
  target: 'Eric',
  attributes: {type: 'KNOWS'}
}

Graph

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

  • object attributes: containing the attributes of the graph (can be omitted).
  • object options: containing the options of the graph (allowSelfLoops, multi and type).
  • 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.mergeEdgeWithKey('T->E', 'Thomas', 'Eric', {type: 'KNOWS'});
graph.setAttribute('name', 'My Graph');
graph.export();
>>> {
  attributes: {
    name: 'My Graph'
  },
  options: {
    allowSelfLoops: true,
    multi: false,
    type: 'mixed'
  },
  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.

#.export

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

Example

graph.mergeEdgeWithKey('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'}
    }
  ]
}