json_pointer 0.0.1 json_pointer: ^0.0.1 copied to clipboard
A simple library for creating and using json-pointer (See [rfc6901:https://tools.ietf.org/html/rfc6901]).
JSON Pointer #
A simple library for creating and using json-pointer (See rfc6901).
Examples #
// Json Pointer Manipulation
compose(['one', 'two']); // '/one/two'
split('/one/two'); // ['one', 'two']
parent('/one/two'); // '/one'
append('/one', 'two'); // '/one/two'
contains('/0/foo', [{'foo': 'bar'}]); // true
isValid('/one/two'); // true
// Json Pointer Part manipulation
escape('one/two'); // 'one~1two'
unescape('one~1two'); // 'one/two'
// Get and Set values in a json object
var obj = [
{'foo': 'bar'}
]
get$('/0/foo', obj); // 'bar'
set$('/0/foo', obj, 'baz'); // [{'foo': 'baz'}]
Escaping Examples #
// Compose escapes parts
compose(['one/two', 'three']); // '/one~1two/three'
// Append escapes the added part
append('/one', 'two/three'); // '/one/two~1three'
// Split unescapes parts
split('/one~1two/three'); // ['one/two', 'three']
Get
, Set
, and Contains
#
The library is only able to index into Map
and List
. get$
and set$
will throw BadRouteError
when attempting to index into other types and contains
will return false.
class Point {
Point(this.x, this.y);
int x;
int y;
}
contains('/x', Point(0, 0)) // false
contains('', Point(0, 0)) // true, this is ok as Point is never indexed
BadRouteError
#
get$
and set$
throw BadRouteError
if the pointer doesn't refer to valid route through json
. an object in the 'root' parameter (e.g. contains(path, root) == false). Set can only be used to change an existing value. It will not attempt to insert into a List
or Map
.
get('/3', [0, 1, 2]) // BadRouteError
set('/3', [0, 1, 2], 'three') // BadRouteError
set('/foo', {}, 'bar') // BadRouteError
PointerFormatError
and PartFormatError
#
All functions that accept a json pointer as an argument will throw PointerFormatError
if not isValid(pointer)
. Similarly, unescape
checks for a valid part and throws PartFormatError
if the format is invalid.
split('0/foo') // PointerFormatError
split('~2/foo') // PointerFormatError:
split(null) // PointerFormatError: null pointer
unescape('/') // PointerFormatError: part cannot contain the '/' character