|
1 |
| -# `json-pointer` |
| 1 | +# JSON Pointer - `json-pointer` |
2 | 2 |
|
3 |
| -Fast implementation of [JSON Pointer (RFC 6901)](https://datatracker.ietf.org/doc/html/rfc6901) in TypeScript. |
| 3 | +Fast implementation of [JSON Pointer (RFC 6901)][json-pointer] |
| 4 | +specification in TypeScript. |
| 5 | + |
| 6 | +[json-pointer]: https://tools.ietf.org/html/rfc6901 |
| 7 | + |
| 8 | + |
| 9 | +## Usage |
4 | 10 |
|
5 | 11 | Can find a value in a JSON object using three methods: (1) JSON Pointer string,
|
6 | 12 | (2) array of steps, or (3) a pre-compiled function.
|
| 13 | + |
| 14 | + |
| 15 | +## Examples |
| 16 | + |
| 17 | +Find the value in a JSON document at some specific location. |
| 18 | + |
| 19 | + |
| 20 | +### Find by JSON Pointer string |
| 21 | + |
| 22 | +```js |
| 23 | +import { findByPointer } from '@jsonjoy.com/json-pointer'; |
| 24 | + |
| 25 | +const doc = { |
| 26 | + foo: { |
| 27 | + bar: 123, |
| 28 | + }, |
| 29 | +}; |
| 30 | + |
| 31 | +const res = findByPointer(doc, '/foo/bar'); |
| 32 | +``` |
| 33 | + |
| 34 | + |
| 35 | +### Find by path array |
| 36 | + |
| 37 | +Alternatively, you can specify an array of steps, such as `['foo', 'bar']`. Or, |
| 38 | +use the `parseJsonPointer` function to convert a JSON Pointer string to an array. |
| 39 | + |
| 40 | +```js |
| 41 | +import { find, parseJsonPointer } from '@jsonjoy.com/json-pointer'; |
| 42 | + |
| 43 | +const doc = { |
| 44 | + foo: { |
| 45 | + bar: 123, |
| 46 | + }, |
| 47 | +}; |
| 48 | + |
| 49 | +const path = parseJsonPointer('/foo/bar'); |
| 50 | +const ref = find(doc, path); |
| 51 | + |
| 52 | +console.log(ref); |
| 53 | +// { val: 123, obj: { bar: 123 }, key: 'bar' } |
| 54 | +``` |
| 55 | + |
| 56 | + |
| 57 | +### Pre-compiled function |
| 58 | + |
| 59 | +If you know the path in advance, you can compile a function that will find the |
| 60 | +value at that location, it will work few times faster than the previous methods. |
| 61 | + |
| 62 | +```js |
| 63 | +import { $$find } from '@jsonjoy.com/json-pointer/lib/codegen'; |
| 64 | + |
| 65 | +const doc = { |
| 66 | + foo: { |
| 67 | + bar: 123, |
| 68 | + }, |
| 69 | +}; |
| 70 | +const finder = $$find(['foo', 'bar']); |
| 71 | + |
| 72 | +const res = finder(doc); |
| 73 | +``` |
| 74 | + |
| 75 | + |
| 76 | +## Low-level API |
| 77 | + |
| 78 | +Convert JSON Pointer to path array and back. |
| 79 | + |
| 80 | +```js |
| 81 | +import { parseJsonPointer } from '@jsonjoy.com/json-pointer'; |
| 82 | + |
| 83 | +console.log(parseJsonPointer('/f~0o~1o/bar/1/baz')); |
| 84 | +// [ 'f~o/o', 'bar', '1', 'baz' ] |
| 85 | + |
| 86 | +console.log(formatJsonPointer(['f~o/o', 'bar', '1', 'baz'])); |
| 87 | +// /f~0o~1o/bar/1/baz |
| 88 | +``` |
| 89 | + |
| 90 | +Decode and encode a single step of JSON Pointer. |
| 91 | + |
| 92 | +```js |
| 93 | +console.log(unescapeComponent('~0~1')); |
| 94 | +// ~/ |
| 95 | + |
| 96 | +console.log(escapeComponent('~/')); |
| 97 | +// ~0~1 |
| 98 | +``` |
0 commit comments