Skip to content
This repository was archived by the owner on Nov 19, 2023. It is now read-only.

gms1/jsonpointerx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

f693609 · Nov 19, 2023
Jun 24, 2022
Jun 24, 2022
Jun 24, 2022
Oct 22, 2019
Aug 18, 2021
Oct 24, 2017
Jun 1, 2019
Oct 24, 2017
May 16, 2021
Jun 12, 2021
Oct 24, 2017
May 16, 2021
Jan 14, 2021
Dec 31, 2017
Jun 22, 2022
Mar 8, 2019
Jun 22, 2022
Jan 26, 2020
Jun 13, 2021
Nov 19, 2023
Oct 25, 2017
Jun 6, 2023
Jun 24, 2022
Oct 25, 2017
Jan 14, 2021
Oct 25, 2017
Jan 12, 2019

Repository files navigation

jsonpointerx

THIS PROJECT HAS BEEN MOVED!

THE NEW LOCATION IS HERE: jsonpointerx

Version Build Workflow Coverage Status DeepScan Grade Known Vulnerabilities

License PRs Welcome

jsonpointerx is one of the x-th jsonpointer implementation The reason I started this project was the need for fast 'get / set' methods via JSON pointers ( see benchmark below )

Introduction

Installation

npm install jsonpointerx

Usage

import {JsonPointer} from 'jsonpointerx';

let content = { foo: ['bar', 'baz'], more: {x: 'y'} };
let jp = JsonPointer.compile('/foo/0');
let jp2 = JsonPointer.compile('/more');
let jp3 = new JsonPointer(['add','new']);    // another way to instantiate a JsonPointer using decoded path segments
                                             // (property names)

jp.get(content);                             // returns 'bar' (content.foo[0])

jp.set(content, 'bak');                      // sets content.foo[0] to 'bak'
jp.set(content);                             // deletes content.foo[0] (does not change the length of the array)
jp2.set(content);                            // deletes content.more

jp3.set(content, {key: 'value'});            // sets content.add.new.key to 'value'

jp.toString();                               // returns '/foo/0'
jp.toURIFragmentIdentifier();                // returns '#/foo/0'

jp2.concat(jp3).toString();                  // returns '/more/add/new'
jp2.concatSegment('add').toString();         // returns '/more/add'
jp2.concatSegment(['add','new']).toString(); // returns '/more/add/new'
jp2.concatPointer('/add/new').toString();    // returns '/more/add/new'

NOTE: the 'get' method should never throw

for convenience these further static methods exist:

JsonPointer.set(content, '/foo/0', 'bar');      // sets content.foo[0] to 'bar'
JsonPointer.get(content, '/foo/0');             // returns 'bar' (content.foo[0])

NOTE: feel free to contribute if you have additional requirements

Benchmark

gms@orion:~/work/HOT/jsonpointerx/bench$ npm run test

============================================================
json pointer: get defined property - suite:
------------------------------------------------------------
  4 tests completed.

  json_pointer.get x     538,688 ops/sec ±0.19% (100 runs sampled)
  jsonpointer.get  x   5,282,555 ops/sec ±0.43% (94 runs sampled)
  json-ptr.get     x 893,703,300 ops/sec ±0.74% (88 runs sampled)
  jsonpointerx.get x 964,196,465 ops/sec ±1.93% (87 runs sampled)

============================================================
json pointer: get property from 'null' ancestor - suite:
------------------------------------------------------------
  4 tests completed.

  json_pointer.get x     125,478 ops/sec ±0.83% (96 runs sampled)
  jsonpointer.get  x      81,552 ops/sec ±0.17% (97 runs sampled)
  json-ptr.get     x 879,120,006 ops/sec ±0.26% (88 runs sampled)
  jsonpointerx.get x 882,737,697 ops/sec ±0.96% (93 runs sampled)

============================================================
json pointer: set property - suite:
------------------------------------------------------------
  4 tests completed.

  json_pointer.set x    492,791 ops/sec ±1.98% (95 runs sampled)
  jsonpointer.set  x  4,088,610 ops/sec ±0.41% (94 runs sampled)
  json-ptr.set     x  6,012,530 ops/sec ±0.28% (94 runs sampled)
  jsonpointerx.set x 14,032,516 ops/sec ±0.65% (99 runs sampled)

NOTE: while 'json-ptr' is now similar fast for get operations, 'jsonpointerx' is about 2 times faster for the set operation

Security

NOTE: please do not feed this library with unsanitized user input

NOTE: sometimes the use of new Function('...') is forbidden (e.g using strict content-security-policy) so you may want to disable this feature by setting the global 'noCompile' option to 'true':

JsonPointer.options({noCompile: true});

NOTE: you can blacklist certain JSON pointer segments. By default only __proto__ and prototype are blacklisted

e.g to add 'foo' to the blacklist:

JsonPointer.options().blacklist.push('foo');

or

JsonPointer.options({blacklist: ['__proto__', 'prototype', 'foo']});

License

jsonpointerx is licensed under the MIT License: LICENSE

Release Notes

CHANGELOG

About

A performant implementation of JSON Pointer (RFC 6901)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published