json-pointer-rfc6901
json-pointer-rfc6901 provides convenient methods for handling json pointers as defined by RFC6901
Installation
Node
npm install json-pointer-rfc6901
Web
bower install json-pointer-rfc6901
Usage
node
var pointer = ; console;
web (global)
web (amd)
require;
API
methods
.get(object, pointer, [options]) -> value
Finds the value in object
as specified by pointer
.
console;// result: 1 console;// result: undefined
.set(object, pointer, value, [options]) -> object
Sets the location in object
, specified by pointer
, to value
.
Returns the modified object
.
var obj = a: 1 ; console;// result: { a: 2 } console;// result: { a: 2, b: 3 } console;// result: { a: 2, b: 3, c: [ { a: 4 } ] } console;// result: { a: 2, b: 3, c: [ { a: 4 }, { b: 5 } ] }
.has(object, pointer, [options]) -> Boolean
Returns true iff the location, specified by pointer
, exists in object
.
console;// result: true console;// result: false
.del(object, pointer, [options]) -> object/undefined
Removes the location, specified by pointer
, from object
.
Returns the modified object
, or undefined if the pointer
is empty.
var obj = a: 1 ; console;// result: { a: 1 } console;// result: {} console;// result: undefined
.escape(str) -> str
Escapes the given path segment as described by RFC6901.
Notably, '~'
's are replaced with '~0'
and '/'
's are replaced with '~1'
console;// result: abc console;// result: ~0 console;// result: ~1
.escapeFragment(str) -> str
Escapes the given path fragment segment as described by RFC6901.
Notably, '~'
's are replaced with '~0'
and '/'
's are replaced with '~1'
and finally the string is URI encoded.
console;// result: a%20b
.unescape(str) -> str
Un-Escapes the given path segment, reversing the actions of .escape
Notably, '~1'
's are replaced with '/'
and '~0'
's are replaced with '~'
console;// result: abc console;// result: ~ console;// result: /
.unescapeFragment(str) -> str
Un-Escapes the given path fragment segment, reversing the actions of .escapeFragment
.
Notably, the string is URI decoded and then '~1'
's are replaced with '/'
and '~0'
's are replaced with '~'
.
console;// result: a b
.isPointer(str) -> Boolean
Returns true iff str
is a valid json pointer value
console;// result: true
.isFragment(str) -> Boolean
Returns true iff str
is a valid json fragment pointer value
console;// result: true
.parse(str) -> Array
Parses a json-pointer or json fragment pointer, as described by RFC901, into an array of path segments.
console;// result: [ 'abc' ] console;// result: [ 'abc' ]
.parsePointer(str) -> Array
Parses a json-pointer, as described by RFC901, into an array of path segments.
console;// result: [ 'abc' ]
.parseFragment(str) -> Array
Parses a json-pointer or json fragment pointer, as described by RFC901, into an array of path segments.
console;// result: [ 'abc' ]
.compile(array) -> str
Converts an array of path segments into a json pointer.
This method is the reverse of .parsePointer
console;// result: /abc console;// result: '/~0/~1/abc' console;// result: '/' console;// result:
.compilePointer(array) -> str
Converts an array of path segments into a json pointer.
This method is the reverse of .parsePointer
console;// result: /abc
.compileFragment(array) -> str
Converts an array of path segments into a json pointer.
This method is the reverse of .compileFragment
console;// result: #/abc
pointer(object, [pointer, [value]]) -> pointer/value/object
Convenience function for choosing between .smartBind
, .get
, and .set
, depending on the number of arguments.
var obj = a: 1 ; console;// result: { a: 2 } console;// result: 2 var bound = ; console;// result: 2 console;// result: 2 console;// result: { a: 3 } console;// result: { a: 4 }
.smartBind(bindings) -> pointer
Creates a clone of the api, with ./.get/.has/.set/.del/.smartBind
method signatures adjusted.
var obj = a: 1 ; console;// result: 1 console;// result: 1 console;// result: 1 console;// result: 1
bound.pointer(str) -> strings
get/set bound pointer value
bound.fragment(str) -> strings
get/set bound pointer value as fragment
bound.object(str) -> strings
get/set bound object
bound.options(str) -> strings
get/set bound options
.hasJsonProp(object, key) -> Boolean
Returns true iff obj
contains key
and obj
is either an Array or an Object.
Ignores the prototype chain.
Default value for options.hasProp
.
.hasOwnProp(object, key) -> Boolean
Returns true iff obj
contains key
, disregarding the prototype chain.
.hasProp(object, key) -> Boolean
Returns true iff obj
contains key
, including via the prototype chain.
.getProp(object, key) -> value
Finds the given key
in obj
.
Default value for options.getProp
.
.setProp(object, key, value) -> value
Sets the given key
in obj
to value
.
Default value for options.setProp
.
.getNotFound(object, segment, root, segments, iSegment) -> value
Returns the value to use when .get
fails to locate a pointer segment.
Default value for options.getNotFound
.
.setNotFound(object, segment, root, segments, iSegment) -> value
Returns the value to use when .set
fails to locate a pointer segment.
Default value for options.setNotFound
.
.delNotFound(object, segment, root, segments, iSegment) -> value
Performs an action when .del
fails to locate a pointer segment.
Default value for options.delNotFound
.
.errorNotFound(object, segment, root, segments, iSegment) -> n/a
Raises a JsonPointerError when the given pointer segment is not found.
May be used in place of the above methods via the options
argument of ./.get/.set/.has/.del/.simpleBind
.
try console;catch ex console;// exception: JsonPointerError : Unable to find json path: /b console;// result: undefinedconsole;// result: 3console;// result: 3 console;// result: undefinedconsole;// result: undefinedconsole;// result: function push() { [native code] }