Skip to content

Commit

Permalink
Return null instead of throwing an exception when the pointer is vali…
Browse files Browse the repository at this point in the history
…d, but the doc does not contain the given value.
  • Loading branch information
hildjj committed Jan 20, 2013
1 parent f1b7967 commit ea7d1ce
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This is an implementation of [JSON Pointer](http://tools.ietf.org/html/draft-iet
var three = jsonpointer.get(obj, "/qux/0");
var four = jsonpointer.get(obj, "/qux/1");
var five = jsonpointer.get(obj, "/qux/2");
var notfound = jsonpointer.get(obj, "/quo"); // returns null

jsonpointer.set(obj, "/foo", 6); // obj.foo = 6;

Expand Down
5 changes: 2 additions & 3 deletions jsonpointer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ var untilde = function(str) {
var traverse = function(obj, pointer, value) {
// assert(isArray(pointer))
var part = untilde(pointer.shift());
if(typeof obj[part] === "undefined") {
throw("Value for pointer '" + pointer + "' not found.");
return;
if(!obj.hasOwnProperty(part)) {
return null;
}
if(pointer.length !== 0) { // keep traversin!
return traverse(obj[part], pointer, value);
Expand Down
17 changes: 7 additions & 10 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ assert.equal(jsonpointer.get(obj, "/d/e/2/c"), 6);

assert.equal(jsonpointer.get(obj, ""), obj);
assert.throws(function() {
assert.equal(jsonpointer.get(obj, "a"), 3);
jsonpointer.get(obj, "a");
});
assert.throws(function() {
jsonpointer.get(obj, "a/");
});

var complexKeys = {
Expand All @@ -51,19 +54,13 @@ assert.equal(jsonpointer.get(complexKeys, "/a~1b/c"), 1);
assert.equal(jsonpointer.get(complexKeys, "/d/e~1f"), 2);
assert.equal(jsonpointer.get(complexKeys, "/~01"), 3);
assert.equal(jsonpointer.get(complexKeys, "/01"), 4);
assert.throws(function() {
assert.equal(jsonpointer.get(complexKeys, "/a/b/c"), 1);
});
assert.throws(function() {
assert.equal(jsonpointer.get(complexKeys, "/~1"), 3);
});
assert.equal(jsonpointer.get(complexKeys, "/a/b/c"), null);
assert.equal(jsonpointer.get(complexKeys, "/~1"), null);

// draft-ietf-appsawg-json-pointer-08 has special array rules
var ary = [ "zero", "one", "two" ];
assert.equal(jsonpointer.get(ary, "/01"), null);

assert.throws(function() {
assert.equal(jsonpointer.get(ary, "/01"), "one");
});
//assert.equal(jsonpointer.set(ary, "/-", "three"), null);
//assert.equal(ary[3], "three");

Expand Down

0 comments on commit ea7d1ce

Please sign in to comment.