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; } }
n/a
eval = function (obj, expr, opts) { return JSONPath(opts, expr, obj); }
...
## 0.11.0 (Dec 12, 2015)
- Breaking change: For unwrapped results, return `undefined` instead
of `false` upon failure to find path (to allow distinguishing of
`undefined`--a non-allowed JSON value--from the valid JSON values,
`null` or `false`) and return the exact value upon falsy single
results (in order to allow return of `null`)
- Deprecated: Use of `jsonPath.eval()`; use new class-based API instead
- Feature: AMD export
- Feature: By using `self` instead of `window` export, allow JSONPath
to be trivially imported into web workers, without breaking
compatibility in normal scenarios. See [MDN on self](https://developer.mozilla.org/en-US/docs/Web/API/Window/self)
- Feature: Offer new class-based API and object-based arguments (with
option to run new queries via `evaluate()` method without resupplying config)
- Feature: Allow new `preventEval=true` and `autostart=false` option
...
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]; }
...
accept any of the other allowed instance properties (except
for `autostart` which would have no relevance here).
## Class properties and methods
- ***JSONPath.cache*** - Exposes the cache object for those who wish
to preserve and reuse it for optimization purposes.
- ***JSONPath.toPathArray(pathAsString)*** - Accepts a normalized or
unnormalized path as string and converts to an array: for
example, `['$', 'aProperty', 'anotherProperty']`.
- ***JSONPath.toPathString(pathAsArray)*** - Accepts a path array and
converts to a normalized path string. The string will be in a form
like: `$['aProperty']['anotherProperty][0]`. The JSONPath terminal
constructions `~` and `^` and type operators like `@string()` are
silently stripped.
...
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; }
...
## 0.13.1 (Jan 5, 2016)
- Fix: Avoid double-encoding path in results
## 0.13.0 (Dec 13, 2015)
- Breaking change (from version 0.11): Silently strip `~` and `^` operators
and type operators such as `@string()` in `JSONPath.toPathString()` calls.
- Breaking change: Remove `Array.isArray` polyfill as no longer
supporting IE <= 8
- Feature: Allow omission of options first argument to `JSONPath`
- Feature: Add `JSONPath.toPointer()` and "pointer" `resultType` option.
- Fix: Correctly support `callback` and `otherTypeCallback` as numbered
arguments to `JSONPath`.
- Fix: Enhance Node checking to avoid issue reported with angular-mock
...
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; }
...
## 0.13.0 (Dec 13, 2015)
- Breaking change (from version 0.11): Silently strip `~` and `^` operators
and type operators such as `@string()` in `JSONPath.toPathString()` calls.
- Breaking change: Remove `Array.isArray` polyfill as no longer
supporting IE <= 8
- Feature: Allow omission of options first argument to `JSONPath`
- Feature: Add `JSONPath.toPointer()` and "pointer" `resultType` option.
- Fix: Correctly support `callback` and `otherTypeCallback` as numbered
arguments to `JSONPath`.
- Fix: Enhance Node checking to avoid issue reported with angular-mock
- Fix: Allow for `@` or other special characters in at-sign-prefixed
property names (by use of `[?(@['...'])]` or `[(@['...'])]`).
## 0.12.0 (Dec 12, 2015 10:39pm)
...