forked from JSONPath-Plus/JSONPath
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonpath.js
More file actions
532 lines (496 loc) · 20.4 KB
/
Copy pathjsonpath.js
File metadata and controls
532 lines (496 loc) · 20.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/*global exports, require*/
/* eslint-disable no-eval */
/* JSONPath 0.8.0 - XPath for JSON
*
* Copyright (c) 2007 Stefan Goessner (goessner.net)
* Licensed under the MIT (MIT-LICENSE.txt) licence.
*/
var module;
(function (glbl, require) {'use strict';
// Make sure to know if we are in real node or not (the `require` variable
// could actually be require.js, for example.
var isNode = module && !!module.exports;
var allowedResultTypes = ['value', 'path', 'pointer', 'parent', 'parentProperty', 'all'];
if (!Array.prototype.includes) {
Array.prototype.includes = function (item) { // eslint-disable-line no-extend-native
return this.indexOf(item) > -1;
};
}
if (!String.prototype.includes) {
String.prototype.includes = function (item) { // eslint-disable-line no-extend-native
return this.indexOf(item) > -1;
};
}
var moveToAnotherArray = function (source, target, conditionCb) {
for (var i = 0, kl = source.length; i < kl; i++) {
var key = source[i];
if (conditionCb(key)) {
target.push(source.splice(i--, 1)[0]);
}
}
};
var vm = isNode
? require('vm') : {
runInNewContext: function (expr, context) {
var keys = Object.keys(context);
var funcs = [];
moveToAnotherArray(keys, funcs, function (key) {
return typeof context[key] === 'function';
});
var code = funcs.reduce(function (s, func) {
return 'var ' + func + '=' + context[func].toString() + ';' + s;
}, '');
code += keys.reduce(function (s, vr) {
return 'var ' + vr + '=' + JSON.stringify(context[vr]).replace(/\u2028|\u2029/g, function (m) {
// http://www.thespanner.co.uk/2011/07/25/the-json-specification-is-now-wrong/
return '\\u202' + (m === '\u2028' ? '8' : '9');
}) + ';' + s;
}, expr);
return eval(code);
}
};
function push (arr, elem) {arr = arr.slice(); arr.push(elem); return arr;}
function unshift (elem, arr) {arr = arr.slice(); arr.unshift(elem); return arr;}
function NewError (value) {
this.avoidNew = true;
this.value = value;
this.message = 'JSONPath should not be called with "new" (it prevents return of (unwrapped) scalar values)';
}
function JSONPath (opts, expr, obj, callback, otherTypeCallback) {
if (!(this instanceof JSONPath)) {
try {
return new JSONPath(opts, expr, obj, callback, otherTypeCallback);
}
catch (e) {
if (!e.avoidNew) {
throw e;
}
return e.value;
}
}
if (typeof opts === 'string') {
otherTypeCallback = callback;
callback = obj;
obj = expr;
expr = opts;
opts = {};
}
opts = opts || {};
var objArgs = opts.hasOwnProperty('json') && opts.hasOwnProperty('path');
this.json = opts.json || obj;
this.path = opts.path || expr;
this.resultType = (opts.resultType && opts.resultType.toLowerCase()) || 'value';
this.flatten = opts.flatten || false;
this.wrap = opts.hasOwnProperty('wrap') ? opts.wrap : true;
this.sandbox = opts.sandbox || {};
this.preventEval = opts.preventEval || false;
this.parent = opts.parent || null;
this.parentProperty = opts.parentProperty || null;
this.callback = opts.callback || callback || null;
this.otherTypeCallback = opts.otherTypeCallback || otherTypeCallback || function () {
throw new Error('You must supply an otherTypeCallback callback option with the @other() operator.');
};
if (opts.autostart !== false) {
var ret = this.evaluate({
path: (objArgs ? opts.path : expr),
json: (objArgs ? opts.json : obj)
});
if (!ret || typeof ret !== 'object') {
throw new NewError(ret);
}
return ret;
}
}
// PUBLIC METHODS
JSONPath.prototype.evaluate = function (expr, json, callback, otherTypeCallback) {
var self = this,
flatten = this.flatten,
wrap = this.wrap,
currParent = this.parent,
currParentProperty = this.parentProperty;
this.currResultType = this.resultType;
this.currPreventEval = this.preventEval;
this.currSandbox = this.sandbox;
callback = callback || this.callback;
this.currOtherTypeCallback = otherTypeCallback || this.otherTypeCallback;
json = json || this.json;
expr = expr || this.path;
if (expr && typeof expr === 'object') {
if (!expr.path) {
throw new Error('You must supply a "path" property when providing an object argument to JSONPath.evaluate().');
}
json = expr.hasOwnProperty('json') ? expr.json : json;
flatten = expr.hasOwnProperty('flatten') ? expr.flatten : flatten;
this.currResultType = expr.hasOwnProperty('resultType') ? expr.resultType : this.currResultType;
this.currSandbox = expr.hasOwnProperty('sandbox') ? expr.sandbox : this.currSandbox;
wrap = expr.hasOwnProperty('wrap') ? expr.wrap : wrap;
this.currPreventEval = expr.hasOwnProperty('preventEval') ? expr.preventEval : this.currPreventEval;
callback = expr.hasOwnProperty('callback') ? expr.callback : callback;
this.currOtherTypeCallback = expr.hasOwnProperty('otherTypeCallback') ? expr.otherTypeCallback : this.currOtherTypeCallback;
currParent = expr.hasOwnProperty('parent') ? expr.parent : currParent;
currParentProperty = expr.hasOwnProperty('parentProperty') ? expr.parentProperty : currParentProperty;
expr = expr.path;
}
currParent = currParent || null;
currParentProperty = currParentProperty || null;
if (Array.isArray(expr)) {
expr = JSONPath.toPathString(expr);
}
if (!expr || !json || !allowedResultTypes.includes(this.currResultType)) {
return;
}
this._obj = json;
var exprList = JSONPath.toPathArray(expr);
if (exprList[0] === '$' && exprList.length > 1) {exprList.shift();}
this._hasParentSelector = null;
var result = this._trace(exprList, json, ['$'], currParent, currParentProperty, callback);
result = result.filter(function (ea) {return ea && !ea.isParentSelector;});
if (!result.length) {return wrap ? [] : undefined;}
if (result.length === 1 && !wrap && !Array.isArray(result[0].value)) {
return this._getPreferredOutput(result[0]);
}
return result.reduce(function (result, ea) {
var valOrPath = self._getPreferredOutput(ea);
if (flatten && Array.isArray(valOrPath)) {
result = result.concat(valOrPath);
}
else {
result.push(valOrPath);
}
return result;
}, []);
};
// PRIVATE METHODS
JSONPath.prototype._getPreferredOutput = function (ea) {
var resultType = this.currResultType;
switch (resultType) {
case 'all':
ea.path = typeof ea.path === 'string' ? ea.path : JSONPath.toPathString(ea.path);
return ea;
case 'value': case 'parent': case 'parentProperty':
return ea[resultType];
case 'path':
return JSONPath.toPathString(ea[resultType]);
case 'pointer':
return JSONPath.toPointer(ea.path);
}
};
JSONPath.prototype._handleCallback = function (fullRetObj, callback, type) {
if (callback) {
var preferredOutput = this._getPreferredOutput(fullRetObj);
fullRetObj.path = typeof fullRetObj.path === 'string' ? fullRetObj.path : JSONPath.toPathString(fullRetObj.path);
callback(preferredOutput, type, fullRetObj);
}
};
JSONPath.prototype._trace = function (expr, val, path, parent, parentPropName, callback, literalPriority) {
// No expr to follow? return path and value as the result of this trace branch
var retObj, self = this;
if (!expr.length) {
retObj = {path: path, value: val, parent: parent, parentProperty: parentPropName};
this._handleCallback(retObj, callback, 'value');
return retObj;
}
var loc = expr[0], x = expr.slice(1);
// We need to gather the return value of recursive trace calls in order to
// do the parent sel computation.
var ret = [];
function retPush (elem) {
ret.push(elem);
}
function addRet (elems) {
if (Array.isArray(elems)) {
elems.forEach(retPush);
} else {
ret.push(elems);
}
}
if ((typeof loc !== 'string' || literalPriority) && val && Object.prototype.hasOwnProperty.call(val, loc)) { // simple case--directly follow property
addRet(this._trace(x, val[loc], push(path, loc), val, loc, callback));
}
else if (loc === '*') { // all child properties
this._walk(loc, x, val, path, parent, parentPropName, callback, function (m, l, x, v, p, par, pr, cb) {
addRet(self._trace(unshift(m, x), v, p, par, pr, cb, true));
});
}
else if (loc === '..') { // all descendent parent properties
addRet(this._trace(x, val, path, parent, parentPropName, callback)); // Check remaining expression with val's immediate children
this._walk(loc, x, val, path, parent, parentPropName, callback, function (m, l, x, v, p, par, pr, cb) {
// We don't join m and x here because we only want parents, not scalar values
if (typeof v[m] === 'object') { // Keep going with recursive descent on val's object children
addRet(self._trace(unshift(l, x), v[m], push(p, m), v, m, cb));
}
});
}
// The parent sel computation is handled in the frame above using the
// ancestor object of val
else if (loc === '^') {
// This is not a final endpoint, so we do not invoke the callback here
this._hasParentSelector = true;
return path.length ? {
path: path.slice(0, -1),
expr: x,
isParentSelector: true
} : [];
}
else if (loc === '~') { // property name
retObj = {path: push(path, loc), value: parentPropName, parent: parent, parentProperty: null};
this._handleCallback(retObj, callback, 'property');
return retObj;
}
else if (loc === '$') { // root only
addRet(this._trace(x, val, path, null, null, callback));
}
else if (/^(-?[0-9]*):(-?[0-9]*):?([0-9]*)$/.test(loc)) { // [start:end:step] Python slice syntax
addRet(this._slice(loc, x, val, path, parent, parentPropName, callback));
}
else if (loc.indexOf('?(') === 0) { // [?(expr)] (filtering)
if (this.currPreventEval) {
throw new Error('Eval [?(expr)] prevented in JSONPath expression.');
}
this._walk(loc, x, val, path, parent, parentPropName, callback, function (m, l, x, v, p, par, pr, cb) {
if (self._eval(l.replace(/^\?\((.*?)\)$/, '$1'), v[m], m, p, par, pr)) {
addRet(self._trace(unshift(m, x), v, p, par, pr, cb));
}
});
}
else if (loc[0] === '(') { // [(expr)] (dynamic property/index)
if (this.currPreventEval) {
throw new Error('Eval [(expr)] prevented in JSONPath expression.');
}
// As this will resolve to a property name (but we don't know it yet), property and parent information is relative to the parent of the property to which this expression will resolve
addRet(this._trace(unshift(this._eval(loc, val, path[path.length - 1], path.slice(0, -1), parent, parentPropName), x), val, path, parent, parentPropName, callback));
}
else if (loc[0] === '@') { // value type: @boolean(), etc.
var addType = false;
var valueType = loc.slice(1, -2);
switch (valueType) {
case 'scalar':
if (!val || !(['object', 'function'].includes(typeof val))) {
addType = true;
}
break;
case 'boolean': case 'string': case 'undefined': case 'function':
if (typeof val === valueType) {
addType = true;
}
break;
case 'number':
if (typeof val === valueType && isFinite(val)) {
addType = true;
}
break;
case 'nonFinite':
if (typeof val === 'number' && !isFinite(val)) {
addType = true;
}
break;
case 'object':
if (val && typeof val === valueType) {
addType = true;
}
break;
case 'array':
if (Array.isArray(val)) {
addType = true;
}
break;
case 'other':
addType = this.currOtherTypeCallback(val, path, parent, parentPropName);
break;
case 'integer':
if (val === +val && isFinite(val) && !(val % 1)) {
addType = true;
}
break;
case 'null':
if (val === null) {
addType = true;
}
break;
}
if (addType) {
retObj = {path: path, value: val, parent: parent, parentProperty: parentPropName};
this._handleCallback(retObj, callback, 'value');
return retObj;
}
}
else if (loc[0] === '`' && val && Object.prototype.hasOwnProperty.call(val, loc.slice(1))) { // `-escaped property
var locProp = loc.slice(1);
addRet(this._trace(x, val[locProp], push(path, locProp), val, locProp, callback, true));
}
else if (loc.includes(',')) { // [name1,name2,...]
var parts, i;
for (parts = loc.split(','), i = 0; i < parts.length; i++) {
addRet(this._trace(unshift(parts[i], x), val, path, parent, parentPropName, callback));
}
}
else if (!literalPriority && val && Object.prototype.hasOwnProperty.call(val, loc)) { // simple case--directly follow property
addRet(this._trace(x, val[loc], push(path, loc), val, loc, callback, true));
}
// We check the resulting values for parent selections. For parent
// selections we discard the value object and continue the trace with the
// current val object
if (this._hasParentSelector) {
for (var t = 0; t < ret.length; t++) {
var rett = ret[t];
if (rett.isParentSelector) {
var tmp = self._trace(rett.expr, val, rett.path, parent, parentPropName, callback);
if (Array.isArray(tmp)) {
ret[t] = tmp[0];
for (var tt = 1, tl = tmp.length; tt < tl; tt++) {
t++;
ret.splice(t, 0, tmp[tt]);
}
} else {
ret[t] = tmp;
}
}
}
}
return ret;
};
JSONPath.prototype._walk = function (loc, expr, val, path, parent, parentPropName, callback, f) {
var i, n, m;
if (Array.isArray(val)) {
for (i = 0, n = val.length; i < n; i++) {
f(i, loc, expr, val, path, parent, parentPropName, callback);
}
}
else if (typeof val === 'object') {
for (m in val) {
if (Object.prototype.hasOwnProperty.call(val, m)) {
f(m, loc, expr, val, path, parent, parentPropName, callback);
}
}
}
};
JSONPath.prototype._slice = function (loc, expr, val, path, parent, parentPropName, callback) {
if (!Array.isArray(val)) {return;}
var i,
len = val.length, parts = loc.split(':'),
start = (parts[0] && parseInt(parts[0], 10)) || 0,
end = (parts[1] && parseInt(parts[1], 10)) || len,
step = (parts[2] && parseInt(parts[2], 10)) || 1;
start = (start < 0) ? Math.max(0, start + len) : Math.min(len, start);
end = (end < 0) ? Math.max(0, end + len) : Math.min(len, end);
var ret = [];
for (i = start; i < end; i += step) {
var tmp = this._trace(unshift(i, expr), val, path, parent, parentPropName, callback);
if (Array.isArray(tmp)) {
tmp.forEach(function (t) {
ret.push(t);
});
}
else {
ret.push(tmp);
}
}
return ret;
};
JSONPath.prototype._eval = function (code, _v, _vname, path, parent, parentPropName) {
if (!this._obj || !_v) {return false;}
if (code.includes('@parentProperty')) {
this.currSandbox._$_parentProperty = parentPropName;
code = code.replace(/@parentProperty/g, '_$_parentProperty');
}
if (code.includes('@parent')) {
this.currSandbox._$_parent = parent;
code = code.replace(/@parent/g, '_$_parent');
}
if (code.includes('@property')) {
this.currSandbox._$_property = _vname;
code = code.replace(/@property/g, '_$_property');
}
if (code.includes('@path')) {
this.currSandbox._$_path = JSONPath.toPathString(path.concat([_vname]));
code = code.replace(/@path/g, '_$_path');
}
if (code.match(/@([\.\s\)\[])/)) {
this.currSandbox._$_v = _v;
code = code.replace(/@([\.\s\)\[])/g, '_$_v$1');
}
try {
return vm.runInNewContext(code, this.currSandbox);
}
catch (e) {
console.log(e);
throw new Error('jsonPath: ' + e.message + ': ' + code);
}
};
// PUBLIC CLASS PROPERTIES AND METHODS
// Could store the cache object itself
JSONPath.cache = {};
JSONPath.toPathString = function (pathArr) {
var i, n, x = pathArr, p = '$';
for (i = 1, n = x.length; i < n; i++) {
if (!(/^(~|\^|@.*?\(\))$/).test(x[i])) {
p += (/^[0-9*]+$/).test(x[i]) ? ('[' + x[i] + ']') : ("['" + x[i] + "']");
}
}
return p;
};
JSONPath.toPointer = function (pointer) {
var i, n, x = pointer, p = '';
for (i = 1, n = x.length; i < n; i++) {
if (!(/^(~|\^|@.*?\(\))$/).test(x[i])) {
p += '/' + x[i].toString()
.replace(/\~/g, '~0')
.replace(/\//g, '~1');
}
}
return p;
};
JSONPath.toPathArray = function (expr) {
var cache = JSONPath.cache;
if (cache[expr]) {return cache[expr].concat();}
var subx = [];
var normalized = expr
// Properties
.replace(/@(?:null|boolean|number|string|integer|undefined|nonFinite|scalar|array|object|function|other)\(\)/g, ';$&;')
// Parenthetical evaluations (filtering and otherwise), directly within brackets or single quotes
.replace(/[\['](\??\(.*?\))[\]']/g, function ($0, $1) {return '[#' + (subx.push($1) - 1) + ']';})
// Escape periods and tildes within properties
.replace(/\['([^'\]]*)'\]/g, function ($0, prop) {
return "['" + prop
.replace(/\./g, '%@%')
.replace(/~/g, '%%@@%%') +
"']";
})
// Properties operator
.replace(/~/g, ';~;')
// Split by property boundaries
.replace(/'?\.'?(?![^\[]*\])|\['?/g, ';')
// Reinsert periods within properties
.replace(/%@%/g, '.')
// Reinsert tildes within properties
.replace(/%%@@%%/g, '~')
// Parent
.replace(/(?:;)?(\^+)(?:;)?/g, function ($0, ups) {return ';' + ups.split('').join(';') + ';';})
// Descendents
.replace(/;;;|;;/g, ';..;')
// Remove trailing
.replace(/;$|'?\]|'$/g, '');
var exprList = normalized.split(';').map(function (expr) {
var match = expr.match(/#([0-9]+)/);
return !match || !match[1] ? expr : subx[match[1]];
});
cache[expr] = exprList;
return cache[expr];
};
// For backward compatibility (deprecated)
JSONPath.eval = function (obj, expr, opts) {
return JSONPath(opts, expr, obj);
};
if (typeof define === 'function' && define.amd) {
define(function () {return JSONPath;});
}
else if (isNode) {
module.exports = JSONPath;
}
else {
glbl.jsonPath = { // Deprecated
eval: JSONPath.eval
};
glbl.JSONPath = JSONPath;
}
}(this || self, typeof require === 'undefined' ? null : require));