Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8f818ef
Showing
4 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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" | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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."); |