Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial commit / release

  • Loading branch information...
commit 8f818ef9292846503e20d6a7658d2efea3b4d56a 0 parents
@janl authored
Showing with 126 additions and 0 deletions.
  1. +29 −0 README.md
  2. +50 −0 jsonpointer.js
  3. +14 −0 package.json
  4. +33 −0 test.js
29 README.md
@@ -0,0 +1,29 @@
+# JSON Pointer for nodejs
+
+This is an implementation of [JSON Pointer](http://tools.ietf.org/html/draft-pbryan-zyp-json-pointer-00).
+
+## Usage
+
+ var jsonpointer = require("jsonpointer");
+ var obj = { foo: 1, bar: { baz: 2}, qux: [3, 4, 5]};
+ var one = jsonpointer.get(obj, "/foo");
+ var two = jsonpointer.get(obj, "/bar/baz");
+ var three = jsonpointer.get(obj, "/qux/0");
+ var four = jsonpointer.get(obj, "/qux/1");
+ var five = jsonpointer.get(obj, "/qux/2");
+
+ jsonpointer.set(obj, "/foo", 6); // obj.foo = 6;
+
+## Testing
+
+ $ node test.js
+ All tests pass.
+ $
+
+## Author
+
+(c) 2011 Jan Lehnardt <jan@apache.org>
+
+## License
+
+MIT License.
50 jsonpointer.js
@@ -0,0 +1,50 @@
+var console = require("console");
+
+var traverse = function(obj, pointer, value) {
+ // assert(isArray(pointer))
+ var part = pointer.shift();
+ if(typeof obj[part] !== "undefined") {
+ if(pointer.length == 0) { // we're done
+ if(typeof value !== "undefined") { // set new value, return old value
+ var old_value = obj[part];
+ if(value === null) {
+ delete obj[part];
+ } else {
+ obj[part] = value;
+ }
+ return old_value;
+ } else { // just reading
+ return obj[part];
+ }
+ } else { // keep traversin!
+ return traverse(obj[part], pointer);
+ }
+ } else {
+ throw("Value for pointer '" + pointer + "' not found.");
+ }
+}
+
+var validate_input = function(obj, pointer) {
+ if(typeof obj !== "object") {
+ throw("Invalid input object.");
+ }
+
+ if(!pointer) {
+ throw("Invalid JSON pointer.");
+ }
+}
+
+var get = function(obj, pointer) {
+ validate_input(obj, pointer);
+ pointer = pointer.split("/").slice(1);
+ return traverse(obj, pointer);
+}
+
+var set = function(obj, pointer, value) {
+ validate_input(obj, pointer);
+ pointer = pointer.split("/").slice(1);
+ return traverse(obj, pointer);
+}
+
+exports.get = get
+exports.set = set
14 package.json
@@ -0,0 +1,14 @@
+{ "name" : "jsonpointer"
+, "description" : "Simple JSON Addressing."
+, "tags" : ["util", "simple", "util", "utility"]
+, "version" : "1.0.0"
+, "author" : "Jan Lehnardt <jan@apache.org>"
+, "repository" :
+ { "type" : "git"
+ , "url" : "http://github.com/janl/node-jsonpointer.git"
+ }
+, "bugs" :
+ { "web" : "http://github.com/janl/node-jsonpointer/issues" }
+, "engines" : ["node >= 0.4.9"]
+, "main" : "./jsonpointer"
+}
33 test.js
@@ -0,0 +1,33 @@
+var assert = require("assert");
+var console = require("console");
+var jsonpointer = require("./jsonpointer");
+
+var obj = {
+ a: 1,
+ b: {
+ c: 2
+ },
+ d: {
+ e: [{a:3}, {b:4}, {c:5}]
+ }
+};
+
+assert.equal(jsonpointer.get(obj, "/a"), 1);
+assert.equal(jsonpointer.get(obj, "/b/c"), 2);
+assert.equal(jsonpointer.get(obj, "/d/e/0/a"), 3);
+assert.equal(jsonpointer.get(obj, "/d/e/1/b"), 4);
+assert.equal(jsonpointer.get(obj, "/d/e/2/c"), 5);
+
+assert.equal(jsonpointer.set(obj, "/a"), 1);
+assert.equal(jsonpointer.set(obj, "/b/c"), 2);
+assert.equal(jsonpointer.set(obj, "/d/e/0/a"), 3);
+assert.equal(jsonpointer.set(obj, "/d/e/1/b"), 4);
+assert.equal(jsonpointer.set(obj, "/d/e/2/c"), 5);
+
+assert.equal(jsonpointer.get(obj, "/a"), 1);
+assert.equal(jsonpointer.get(obj, "/b/c"), 2);
+assert.equal(jsonpointer.get(obj, "/d/e/0/a"), 3);
+assert.equal(jsonpointer.get(obj, "/d/e/1/b"), 4);
+assert.equal(jsonpointer.get(obj, "/d/e/2/c"), 5);
+
+console.log("All tests pass.");
Please sign in to comment.
Something went wrong with that request. Please try again.