|
1 | | -/* JSONPath 0.8.0 - XPath for JSON |
2 | | - * |
3 | | - * Copyright (c) 2007 Stefan Goessner (goessner.net) |
4 | | - * Licensed under the MIT (MIT-LICENSE.txt) licence. |
5 | | - */ |
6 | | - |
7 | | -exports.eval = jsonPath; |
8 | | -var cache = {}; |
9 | | -function jsonPath(obj, expr, arg) { |
10 | | - var P = { |
11 | | - resultType: arg && arg.resultType || "VALUE", |
12 | | - result: [], |
13 | | - normalize: function(expr) { |
14 | | - if(cache[expr]) { |
15 | | - return cache[expr]; |
16 | | - } |
17 | | - |
18 | | - var subx = []; |
19 | | - ret = expr.replace(/[\['](\??\(.*?\))[\]']/g, function($0,$1){return "[#"+(subx.push($1)-1)+"]";}) |
20 | | - .replace(/'?\.'?|\['?/g, ";") |
21 | | - .replace(/;;;|;;/g, ";..;") |
22 | | - .replace(/;$|'?\]|'$/g, "") |
23 | | - .replace(/#([0-9]+)/g, function($0,$1){return subx[$1];}); |
24 | | - cache[expr] = ret; |
25 | | - return ret; |
26 | | - }, |
27 | | - asPath: function(path) { |
28 | | - var x = path.split(";"), p = "$"; |
29 | | - for (var i=1,n=x.length; i<n; i++) |
30 | | - p += /^[0-9*]+$/.test(x[i]) ? ("["+x[i]+"]") : ("['"+x[i]+"']"); |
31 | | - return p; |
32 | | - }, |
33 | | - store: function(p, v) { |
34 | | - if (p) P.result[P.result.length] = P.resultType == "PATH" ? P.asPath(p) : v; |
35 | | - return !!p; |
36 | | - }, |
37 | | - trace: function(expr, val, path) { |
38 | | - if (expr) { |
39 | | - var x = expr.split(";"), loc = x.shift(); |
40 | | - x = x.join(";"); |
41 | | - if (val && val.hasOwnProperty(loc)) |
42 | | - P.trace(x, val[loc], path + ";" + loc); |
43 | | - else if (loc === "*") |
44 | | - P.walk(loc, x, val, path, function(m,l,x,v,p) { P.trace(m+";"+x,v,p); }); |
45 | | - else if (loc === "..") { |
46 | | - P.trace(x, val, path); |
47 | | - P.walk(loc, x, val, path, function(m,l,x,v,p) { typeof v[m] === "object" && P.trace("..;"+x,v[m],p+";"+m); }); |
48 | | - } |
49 | | - else if (/,/.test(loc)) { // [name1,name2,...] |
50 | | - for (var s=loc.split(/'?,'?/),i=0,n=s.length; i<n; i++) |
51 | | - P.trace(s[i]+";"+x, val, path); |
52 | | - } |
53 | | - else if (/^\(.*?\)$/.test(loc)) // [(expr)] |
54 | | - P.trace(P.eval(loc, val, path.substr(path.lastIndexOf(";")+1))+";"+x, val, path); |
55 | | - else if (/^\?\(.*?\)$/.test(loc)) // [?(expr)] |
56 | | - P.walk(loc, x, val, path, function(m,l,x,v,p) { if (P.eval(l.replace(/^\?\((.*?)\)$/,"$1"),v[m],m)) P.trace(m+";"+x,v,p); }); |
57 | | - else if (/^(-?[0-9]*):(-?[0-9]*):?([0-9]*)$/.test(loc)) // [start:end:step] phyton slice syntax |
58 | | - P.slice(loc, x, val, path); |
59 | | - } |
60 | | - else |
61 | | - P.store(path, val); |
62 | | - }, |
63 | | - walk: function(loc, expr, val, path, f) { |
64 | | - if (val instanceof Array) { |
65 | | - for (var i=0,n=val.length; i<n; i++) |
66 | | - if (i in val) |
67 | | - f(i,loc,expr,val,path); |
68 | | - } |
69 | | - else if (typeof val === "object") { |
70 | | - for (var m in val) |
71 | | - if (val.hasOwnProperty(m)) |
72 | | - f(m,loc,expr,val,path); |
73 | | - } |
74 | | - }, |
75 | | - slice: function(loc, expr, val, path) { |
76 | | - if (val instanceof Array) { |
77 | | - var len=val.length, start=0, end=len, step=1; |
78 | | - loc.replace(/^(-?[0-9]*):(-?[0-9]*):?(-?[0-9]*)$/g, function($0,$1,$2,$3){start=parseInt($1||start);end=parseInt($2||end);step=parseInt($3||step);}); |
79 | | - start = (start < 0) ? Math.max(0,start+len) : Math.min(len,start); |
80 | | - end = (end < 0) ? Math.max(0,end+len) : Math.min(len,end); |
81 | | - for (var i=start; i<end; i+=step) |
82 | | - P.trace(i+";"+expr, val, path); |
83 | | - } |
84 | | - }, |
85 | | - eval: function(x, _v, _vname) { |
86 | | - try { return $ && _v && eval(x.replace(/@/g, "_v")); } |
87 | | - catch(e) { throw new SyntaxError("jsonPath: " + e.message + ": " + x.replace(/@/g, "_v").replace(/\^/g, "_a")); } |
88 | | - } |
89 | | - }; |
90 | | - |
91 | | - var $ = obj; |
92 | | - if (expr && obj && (P.resultType == "VALUE" || P.resultType == "PATH")) { |
93 | | - P.trace(P.normalize(expr).replace(/^\$;/,""), obj, "$"); |
94 | | - return P.result.length ? P.result : false; |
95 | | - } |
96 | | -} |
| 1 | +/* JSONPath 0.8.0 - XPath for JSON |
| 2 | + * |
| 3 | + * Copyright (c) 2007 Stefan Goessner (goessner.net) |
| 4 | + * Licensed under the MIT (MIT-LICENSE.txt) licence. |
| 5 | + */ |
| 6 | + |
| 7 | +var Environment = require('./environment'); |
| 8 | + |
| 9 | +var JSONPath = module.exports = { |
| 10 | + eval: function jsonPath(jsonObject, expression, options) { |
| 11 | + if(!jsonObject) |
| 12 | + throw 'You must provide a JSON object'; |
| 13 | + if(!expression) |
| 14 | + throw 'An expression is required'; |
| 15 | + if(!options) |
| 16 | + options = {}; |
| 17 | + if(!options.resultType) |
| 18 | + options.resultType = 'VALUE'; |
| 19 | + if(options.resultType != "VALUE" && options.resultType != "PATH") |
| 20 | + throw 'Invalid options, resultType must be "VALUE" or "PATH"'; |
| 21 | + |
| 22 | + var env = new Environment(jsonObject, expression, options); |
| 23 | + return env.execute(); |
| 24 | + } |
| 25 | +}; |
0 commit comments