Skip to content
This repository was archived by the owner on Mar 6, 2018. It is now read-only.

JSON8/patch

 
 

Repository files navigation

JSON8 Patch

build status

JSON Patch RFC 6902 implementation for JavaScript.

See http://jsonpatch.com/ for more information about JSON Patch.

JSON8 Patch passes the entire json-patch-tests suite; see Tests

Getting started

npm install json8-patch

var JSONPatch = require('json8-patch');

or

<script src="node_modules/json8-patch/JSON8Patch.js"></script>
var JSONPatch = window.JSON8Patch

For performance concerns JSON8 Patch mutates documents; if you want it to work on its own shallow copy of your document use:

var clone = require('json8').clone;
doc = clone(doc)

See clone.

Methods

apply

doc = JSONPatch.apply(doc, [
  { "op": "add",     "path": "/tags/0",        "value": "family"          },
  { "op": "remove",  "path": "/height"                                    },
  { "op": "replace", "path": "/age",           "value": "26"              },
  { "op": "move",    "from": "/address/city",  "path": "/address/country" },
  { "op": "copy",    "from": "/starred",       "to":    "bookmarked"      },
  { "op": "test",    "path": "/starred",       "value": true              }
]);

JSONPatch.apply returns a document because the JSON Patch specification states that an operation can replace the original document.

JSONPatch.apply is atomic, if any operation fails, the document will be restored to its original state and an error will be thrown.

patch

Alias for apply method.

revert

If the patch or apply method is called with a third argument {reversible: true} it will return an array of the form [doc, revert].

The revert object can be used to revert a patch on a document.

// apply the patch with the reversible option
var patchResult = JSONPatch.apply(doc, patch, {reversible: true});
doc = patchResult[0];

// revert the patch
doc = JSONPatch.revert(doc, patchResult[1]);
// doc is strictly identical to the origina

Operations

add, copy, replace, move, remove, test operations return an array of the form [document, previous, idx]

The first argument is the patched document.

The second argument is the previous value at the specified destination if any, undefined otherwise.

The third argument is used internaly and can be ignored. It is the index of the new element. For add operation only using the JSON Pointer '-' token to push an item at the end of an array.

add

doc = JSONPatch.add(doc, '/foo', 'foo')[0]

remove

doc = JSONPatch.remove(doc, '/foo')[0];

replace

doc = JSONPatch.replace(doc, '/foo', 'foo')[0];

move

doc = JSONPatch.move(doc, '/foo', '/bar')[0];

copy

doc = JSONPatch.copy(doc, '/foo', '/bar')[0];

test

doc = JSONPatch.test(doc, '/foo', 'bar')[0];

Extra operations

Those are not part of the standard and are only provided for convenience.

get

JSONPatch.get(doc, '/foo');
// returns value at /foo

has

JSONPatch.has(doc, '/foo');
// returns true if foo property exists, false otherwise

Tests

git submodule update --init --recursive
npm install -g eslint mocha babel
npm test

Contributing

See CONTRIBUTING.md

Packages

 
 
 

Contributors