Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Return null when value not found #9

Merged
merged 1 commit into from

3 participants

@hildjj

Throwing an exception is for exceptional cases. Not finding the target data doesn't feel like an exceptional case, particularly if I'm going to use the pointer as a filter to select from a set of objects that might match.

@janl
Owner

Agreed.

Note to self. this breaks BC and we must bump to version 2.x.y.

@janl janl merged commit 99a7865 into janl:master
@hildjj

Agree with BC issue. Wait for compile patch before releasing 2.x though?

@janl
Owner

Can do, but no biggie, I am fast and loose with major version numbers, I wouldn’t mind shipping 2.0.0 tonight and 3.0.0 tomorrow ;)

I’ll play it by ear if anyone wants this in a release asap, if not it can wait.

@benatkin

I can use this fix. What's the compile patch?

@janl
Owner

hm?

@benatkin

It was @hildjj that said "Wait for compile patch before releasing 2.x though?"

Another note, it ought to return undefined rather than null. That would be a convenient way to differentiate between finding "null" and not finding the key at all.

@hildjj

I was talking about hildjj@1c373d5

@janl
Owner

@benatkin good point, wanna open a new issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 20, 2013
  1. @hildjj

    Return null instead of throwing an exception when the pointer is vali…

    hildjj authored
    …d, but the doc does not contain the given value.
This page is out of date. Refresh to see the latest.
Showing with 10 additions and 13 deletions.
  1. +1 −0  README.md
  2. +2 −3 jsonpointer.js
  3. +7 −10 test.js
View
1  README.md
@@ -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;
View
5 jsonpointer.js
@@ -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);
View
17 test.js
@@ -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 = {
@@ -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");
Something went wrong with that request. Please try again.