@version 2.3.0
@date 2017-04-27
@stability 3 - Stable
JSON Pointer RFC 6901 and JSON Merge Patch RFC 7396 implementation. The main goal is to have a small and reasonably fast code.
When used directly with script tag, methods are added to global JSON object.
When used with require
,
methods are exported as expected.
var util = require("json-util")
, obj = {"a": {"b": 1}}
// get a value
var b = util.pointer(obj, "/a/b")
// b is 1
// set a value
var oldValue = util.pointer(obj, "/a/b", 2) // sets value to 2
// oldValue is 1
// adding to a nonexistent target will create missing object literals
util.pointer(obj, "/a/c/d/e", "3")
// obj is now {"a": {"b": 2, "c": {"d": {"e": "3"}}}}
// apply a patch and collect JSON Pointers to array that were changed
var changed = []
util.mergePatch(obj, {"a": {"c": "C"}}, changed /* optional */)
// obj is now {"a": {"b": 2, "c": "C"}}
// changed = ["/a", "/a/c"]
// test is it a object literal
json.isObject(obj)
// true
Copyright (c) 2014-2016 Lauri Rooden <lauri@rooden.ee>
The MIT License
Have an update or suggestion for this note? You can edit it and send me a pull request.
Since 2013 to 2015