Skip to content

Commit

Permalink
jsgraph v0.4.0 (BREAKING CHANGE FOR PERSISTED JSON DATA) ...
Browse files Browse the repository at this point in the history
... simplifies the JSON export format, removes package.json self-reference, and fixes a bug on JSON import. Note that if you have v0.3.0 JSON jsgraph data (e.g. in a database or file) it's not going to parse with v0.4.0 until you modify the outer JSON to: { jsgraph: { digraph: { vertices: [], edges: [] } } (new JSON I/O format).
  • Loading branch information
Chris Russell committed May 27, 2015
1 parent b620c39 commit 124de53
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsgraph",
"version": "0.3.0",
"version": "0.4.0",
"description": "Generic directed graph container, and visitor algorithms based on a port of the Boost C++ Graph Library API.",
"main": "index.js",
"scripts": {
Expand Down
9 changes: 1 addition & 8 deletions src/digraph-json-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
// constructor parameter to restore container state across
// execution contexts.

var packageMeta = require('../package');

module.exports = function (digraph_, replacer_, space_) {

var digraphState = {
Expand All @@ -31,12 +29,7 @@ module.exports = function (digraph_, replacer_, space_) {
outEdges.forEach(processEdge);
}

var jsonExportObject = {
jsgraph: {
version: packageMeta.version,
directed: digraphState
}
};
var jsonExportObject = { jsgraph: { digraph: digraphState } };

return JSON.stringify(jsonExportObject, replacer_, space_);
};
12 changes: 6 additions & 6 deletions src/digraph-json-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,24 @@ module.exports = function (digraph_, json_) {
throw new Error("JSON semantics error: Could not find required top-level object 'jsgraph'.");
}

type = getType(jsonParse.jsgraph.directed);
type = getType(jsonParse.jsgraph.digraph);
if (type !== '[object Object]') {
throw new Error("JSON semantics error: Could not find expected digraph state object 'jsgraph.directed'.");
}

jsonParse = jsonParse.jsgraph.directed;
digraphJSON = jsonParse.jsgraph.digraph;

type = getType(jsonParse.vertices);
type = getType(digraphJSON.vertices);
if (type !== '[object Array]') {
throw new Error("JSON semantics error: Expected 'vertices' to be an array but found '" + type + "'.");
}

type = getType(jsonParse.edges);
type = getType(digraphJSON.edges);
if (type !== '[object Array]') {
throw new Error("JSON semantics error: Expected 'edges' to be an array but found '" + type + "'.");
}

jsonParse.vertices.forEach(function(vertexDescriptor_) {
digraphJSON.vertices.forEach(function(vertexDescriptor_) {
type = getType(vertexDescriptor_);
if (type !== '[object Object]') {
throw new Error("JSON semantics error: Expected vertex descriptor object in 'vertices' array but found '" + type + "' instead.");
Expand All @@ -61,7 +61,7 @@ module.exports = function (digraph_, json_) {
digraph_.addVertex(vertexDescriptor_.vid, vertexDescriptor_.vprops);
});

jsonParse.edges.forEach(function(edgeDescriptor_) {
digraphJSON.edges.forEach(function(edgeDescriptor_) {
digraph_.addEdge(edgeDescriptor_.uid, edgeDescriptor_.vid, edgeDescriptor_.eprops);
});

Expand Down
6 changes: 3 additions & 3 deletions test/digraph-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ describe("DirectedGraph container object tests", function() {
var parsed = JSON.parse(json);
assert.isObject(parsed);
assert.isObject(parsed.jsgraph);
assert.isObject(parsed.jsgraph.directed);
assert.isArray(parsed.jsgraph.directed.vertices);
assert.isArray(parsed.jsgraph.directed.edges);
assert.isObject(parsed.jsgraph.digraph);
assert.isArray(parsed.jsgraph.digraph.vertices);
assert.isArray(parsed.jsgraph.digraph.edges);
});

describe("Re-create the directed graph container from the JSON.", function() {
Expand Down

0 comments on commit 124de53

Please sign in to comment.