ARCcore.graph DirectedGraph Serialization Reference
Login
-Home
/
-Documentation
/
-ARCcore
/
-graph
/
-DirectedGraph
/
Serialization
-DirectedGraph
+Algorithms
Examples
Vertex Methods
Edge Methods
Container Methods
API Objects
Serialization
Importing and Exporting DirectedGraph Container Contents
Serialize/deserialize container contents to/from object(s) and JSON(s).

See also: API Conventions for discussion of vertex and edge read and write parameters

DirectedGraph Export Object

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.

DirectedGraph Serialization

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.

DirectedGraph Deserialization

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
};
Encapsule Project, Seattle WA
Copyright © 2018 Chris Russell
Sat Dec 15 2018 04:21:49 GMT-0500 (EST)

Encapsule/holism v0.0.26
Documents Under Contruction