Skip to content
Browse files

Implement JSON pointer.

Closes #12.
  • Loading branch information...
1 parent 007f370 commit 20aead0e55d52309d6a4fa83e6ac7dcc77dab8b7 @bigeasy committed Mar 2, 2013
Showing with 25 additions and 1 deletion.
  1. +16 −0 README.md
  2. +1 −1 index.js
  3. +8 −0 t/search/json-pointer.t.js
View
16 README.md
@@ -82,6 +82,22 @@ verbosity, not for pattern matching. There is no good way to pattern match
against property names in inquiry. You're expected to know the structure of the
JSON you're querying.
+## JSON Pointer
+
+You can also [JSON
+pointer](http://tools.ietf.org/html/draft-pbryan-zyp-json-pointer-02) which is
+simply URL encoded path parts.
+
+```javascript
+var object = { "@#$%^&": { ">": 0, "%3E": 1 } };
+equal($q("%40%23%24%25%5E%26/%3E")(object).pop(), 0, 'encoded');
+equal($q("%40%23%24%25%5E%26/`%3E")(object).pop(), 1, 'escaped encoding');
+```
+
+I imagine this might be helpful if you want to add paths to URLs, but I've not
+found a use case in the wild. If you do find, one [drop me a
+line](https://github.com/bigeasy/inquiry/issues/12).
+
## Predicates
Curly braces indicate predicates. Each step in the path can include a single
View
2 index.js
@@ -28,7 +28,7 @@
// optional predicate or subquery opener.
$ = /^(\/{1,2})(\.\.|\.|(?:[^[{/`]|`.)*)([{[]?)(.*)/.exec(rest);
if (!$) throw new Error(error(0));
- $[2] = $[2].replace(/`(.)/, "$1");
+ $[2] = decodeURIComponent($[2].replace(/`%/g, '%25')).replace(/`(.)/, "$1");
rest = $[4];
struct = $.slice(1, 3);
// Check for have a predicate or a sub-expression.
View
8 t/search/json-pointer.t.js
@@ -0,0 +1,8 @@
+#!/usr/bin/env node
+
+var inquiry = require("../.."), object;
+require("proof")(2, function (equal) {
+ object = { "@#$%^&": { ">": 1, "%3E": 0 } };
+ equal(inquiry("%40%23%24%25%5E%26/%3E")(object).pop(), 1, 'encoded');
+ equal(inquiry("%40%23%24%25%5E%26/`%3E")(object).pop(), 0, 'escaped');
+});

0 comments on commit 20aead0

Please sign in to comment.
Something went wrong with that request. Please try again.