diff --git a/CHANGES.md b/CHANGES.md index 4326978..5dcffa6 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,5 @@ +## Nov 20, 2012 +* Change ? to + ## Mar 28, 2012 diff --git a/README.md b/README.md index 48ac235..fff836d 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,17 @@ Install ======= - npm install JSONPath + npm install jsonpath-rep Evaluate ======== - var jsonpath = require('JSONPath'); - jsonpath.eval(obj, path); + var jsonpath = require('jsonpath-rep'); + jsonpath.eval(obj, path, args); -Or more concisely: +With replacement - var jsonpath = require('JSONPath').eval; - jsonpath(obj, path); + jsonpath.eval(obj, path, args, replaceval); Examples ======== @@ -63,8 +62,8 @@ XPath | JSONPath | Result | $..book[-1:] | //book[position()<3]| $..book[0,1] | the first two books | $..book[:2] | -//book[isbn] | $..book[?(@.isbn)] | filter all books with isbn number -//book[price<10] | $..book[?(@.price<10)] | filter all books cheapier than 10 +//book[isbn] | $..book[+(@.isbn)] | filter all books with isbn number +//book[price<10] | $..book[+(@.price<10)] | filter all books cheapier than 10 //* | $..* |all Elements in XML document. All members of JSON structure. diff --git a/lib/jsonpath.js b/lib/jsonpath.js index f7a7164..34ac64a 100644 --- a/lib/jsonpath.js +++ b/lib/jsonpath.js @@ -1,5 +1,6 @@ -/* JSONPath 0.8.0 - XPath for JSON - * +/* jsonpath-rep 0.0.1 - XPath for JSON with replacement + * Surat Teerapittayanon + * Modified from JSONPath 0.9.0 by * Copyright (c) 2007 Stefan Goessner (goessner.net) * Licensed under the MIT (MIT-LICENSE.txt) licence. */ @@ -8,126 +9,171 @@ var vm = require('vm'), _ = require('underscore'); exports.eval = jsonPath; var cache = {}; -function jsonPath(obj, expr, arg) { - var P = { - resultType: arg && arg.resultType || "VALUE", - flatten: arg && arg.flatten || false, - wrap: (arg && arg.hasOwnProperty('wrap')) ? arg.wrap : true, - sandbox: (arg && arg.sandbox) ? arg.sandbox : {}, - normalize: function(expr) { - if(cache[expr]) { - return cache[expr]; - } - var subx = []; - ret = expr.replace(/[\['](\??\(.*?\))[\]']/g, function($0,$1){return "[#"+(subx.push($1)-1)+"]";}) - .replace(/'?\.'?|\['?/g, ";") - .replace(/;;;|;;/g, ";..;") - .replace(/;$|'?\]|'$/g, "") - .replace(/#([0-9]+)/g, function($0,$1){return subx[$1];}); - cache[expr] = ret; - return ret; - }, - asPath: function(path) { - var x = path.split(";"), p = "$"; - for (var i=1,n=x.length; i