See also: API Conventions for discussion of vertex and edge read and write parameters
A DirectedGraph
export object is a serializable JavaScript object with the following format:
var digraphExportObject = {
name: "My Digraph Data", // optional string
description: "Description string...", // optional string
vlist: [ /* zero or more vertex write objects... */ ],
elist: [ /* zero or more edge write objects... */ ]
};
vlist
array is order agnostic and contains zero or more vertex write request object(s).
elist
array is order agnostic and contains zero or more edge write request object(s).
Recall that vertex write request and edge write request objects are used for calling DirectedGraph.addVertex
and DirectedGraph.addEdge
respectively. See API Conventions for more detail.
Oftentimes it is useful to serialize the contents of a DirectedGraph
container:
var digraphExportObject = digraph.toJSON(); // returns a serializable object as above
It's a little confusing but, toJSON
returns a serializable object, not a JSON string. This is standard
JavaScript container practice that allows, for example, an array of DirectedGraph
container instances to be
serialized as an array of objects and not an array of escaped strings as would be the case if toJSON
returned
JSON instead of a serializable object.
But, sometimes serializing directly to a JSON string is exactly what you want. In this case, call the stringify
method:
var digraphExportJSON = digraph.stringify(); // accepts standard replacer, space params
The stringify
method accepts optional standard in-params replacer
and space
as detailed in the
JSON.stringify
documentation on MDN.
To construct a DirectedGraph
container from an export object or JSON string, pass the exported data to DirectedGraph
factory function:
var response = jsgraph.directed.create({
vlist: [
{ u: 'Bellevue', p: 'city' },
{ u: 'Seattle', p: 'city' }
],
elist: [
{ e: { u: 'Bellevue', v: 'Seattle' }, p: 'I-520 Bridge Westbound' },
{ e: { u: 'Seattle', v: 'Bellevue' }, p: 'I-520 Bridge Eastbound' }
]
});
if (!response.error) {
var digraph = response.result;
// The container is initialized
}
In some scenarios you may find it useful to build the contents of your DirectedGraph
container up from a collection export objects
(serialized off in a database, filesystem, or even generated programmatically by some other process).
DirectedGraph
methods fromObject
and fromJSON
parse an export object or JSON string respectively and add deserialize on top of
the existing data in the container using the addVertex
and addEdge
methods. You could call these methods yourself but that's
not always convenient.
Vertex write request object
The DirectedGraph
export object property vlist
contains an array of vertex write request objects:
var vertexWriteRequest = {
u: vertex ID string
p: optional serializable data
};
Edge write request object
The DirectedGraph
export object elist
array contains an array of edge write request objects:
var edgeWriteRequest = {
e: {
u: vertex ID string
v: vertex ID string
},
p: optional serializable data
};