Welcome to python-json-patch-ext’s documentation!¶
The jsonpatchext module¶
-
class
jsonpatchext.CheckOperation(operation)¶ Check value by specified location using a comparator.
-
apply(obj)¶ Abstract method that applies a patch operation to the specified object.
-
-
jsonpatchext.EqualsComparator(current, compare)¶ Compare if the values are exactly equals.
-
class
jsonpatchext.JsonPatchExt(patch)¶ A JSON Patch is a list of Patch Operations.
This modules add 3 more operations: ‘check’, ‘mutate’ and ‘merge’.
>>> def StartsWithComparator(current, compare): ... if current.startswith(compare): ... msg = '{0} ({1}) does not starts with {2} ({3})' ... raise JsonPatchTestFailed(msg.format(current, type(current), compare, type(compare))) ... >>> def RemoveLastMutator(current, value): ... return current[:-1] ... >>> patch = JsonPatchExt([ ... {'op': 'add', 'path': '/foo', 'value': {'bar': 'barvalue'}}, ... {'op': 'check', 'path': '/foo/bar', 'value': 'bar', 'cmp': 'equals'}, ... {'op': 'merge', 'path': '/foo', 'value': {'newbar': 'newbarvalue'}}, ... {'op': 'check', 'path': '/foo/newbar', 'value': 'newb', 'cmp': 'custom', 'comparator': StartsWithComparator}, ... {'op': 'mutate', 'path': '/foo/newbar', 'mut': 'uppercase'}, ... {'op': 'mutate', 'path': '/foo/newbar', 'mut': 'custom', 'mutator': RemoveLastMutator}, ... {'op': 'mutate', 'path': '/foo/bar', 'mut': ['uppercase', ('custom', RemoveLastMutator)]}, ... ]) >>> doc = {} >>> result = patch.apply(doc) >>> expected = {'foo': {'bar': 'BARVALU', 'newbar': 'NEWBARVALU'}} >>> result == expected True
-
class
jsonpatchext.MergeOperation(operation)¶ Merges an object property or an array element with a new value, using package deepmerge.
-
apply(obj)¶ Abstract method that applies a patch operation to the specified object.
-
-
jsonpatchext.apply_patch(doc, patch, in_place=False)¶ Apply list of patches to specified json document.
- Parameters
doc (dict) – Document object.
patch (list or str) – JSON patch as list of dicts or raw JSON-encoded string.
in_place (bool) – While
Truepatch will modify target document. By default patch will be applied to document copy.
- Returns
Patched document object.
- Return type
dict
-
jsonpatchext.make_patch(src, dst)¶ Generates patch by comparing two document objects. Actually is a proxy to
JsonPatch.from_diff()method.- Parameters
src (dict) – Data source document object.
dst (dict) – Data source document object.