‘jsondata.JSONPatch’ - Module

The JSONPatch module provides for the alteration of JSON data compliant to RFC6902.

The emphasis of the design combines low resource requirement with features designed for the application of large filters onto large JSON based data structures.

The patch list itself is defined by RFC6902 as a JSON array. The entries could be either constructed in-memory, or imported from a persistent storage. The export feature provides for the persistent storage of a modified patch list for later reuse.

The module contains the following classes:

  • JSONPatch:

    The controller for the application of patches on in-memory data structures provided by the package ‘json’.

  • JSONPatchItem:

    Representation of one patch entry in accordance to RFC6902.

  • JSONPatchItemRaw:

    Representation of one patch entry read as a raw entry in accordance to RFC6902.

  • JSONPatchFilter:

    Selection filter for the application on the current patch list entries JSONPatchItem.

  • JSONPatchException:

    Specific exception for this module.

The address of the the provided ‘path’ components for the entries are managed by the class JSONPointer in accordance to RFC6901.

Functions

getOp

jsondata.JSONPatch.getOp(x)[source]

Converts input into corresponding enumeration.

JSONPatch

class jsondata.JSONPatch.JSONPatch[source]

Representation of a JSONPatch task list for RFC6902.

Contains the defined methods from standards:

  • add
  • remove
  • replace
  • move
  • copy
  • test
Attributes:
patch: List of patch items.

Attributes

  • JSONPatch.data: JSONPatch object data tree.

Methods

__init__

JSONPatch.__init__()[source]

__str__

JSONPatch.__str__()[source]

Prints the display format.

__repr__

JSONPatch.__repr__()[source]

Prints the representation format of a JSON patch list.

apply

JSONPatch.apply(jsondata)[source]

Applies the JSONPatch task.

Args:
jsondata: JSON data the joblist has to be applied on.
Returns:
Returns a tuple of:
0: len of the job list 1: list of the execution status for the tasks
Raises:
JSONPatchException:

get

JSONPatch.get(x=None)[source]

patch_export

JSONPatch.patch_export(patchfile, schema=None, **kargs)[source]

Exports the current task list.

Provided formats are:
RFC6902
Supports the formats:
RFC6902
Args:
patchfile:
JSON patch for export.
schema:
JSON-Schema for validation of the patch list.
**kargs:
validator: [default, draft3, off, ]
Sets schema validator for the data file. The values are: default=validate, draft3=Draft3Validator, off=None. default:= validate
Returns:
When successful returns ‘True’, else raises an exception.
Raises:
JSONPatchException:

patch_import

JSONPatch.patch_import(patchfile, schemafile=None, **kargs)[source]

Imports a task list.

Supports the formats:
RFC6902
Args:
patchfile:
JSON patch filename containing the list of patch operations.
schemafile:
JSON-Schema filename for validation of the patch list.
**kargs:
validator: [default, draft3, off, ]
Sets schema validator for the data file. The values are: default=validate, draft3=Draft3Validator, off=None. default:= validate
Returns:
When successful returns ‘True’, else raises an exception.
Raises:
JSONPatchException:

repr_export

JSONPatch.repr_export()[source]

Prints the export representation format of a JSON patch list.

Operators

‘()’

JSONPatch.__call__(j, x=None)[source]

Evaluates the related task for the provided index.

Args:

x: Task index.

j: JSON data the task has to be
applied on.
Returns:
Returns a tuple of:

0: len of the job list 1: list of the execution status for

the tasks
Raises:
JSONPatchException:

‘[]’

JSONPatch.__getitem__(key)[source]

Support of slices, for ‘iterator’ refer to self.__iter__.

  1. self[key]
  2. self[i:j:k]
  3. x in self
  4. for x in self

‘S+x’

JSONPatch.__add__(x=None)[source]

Creates a copy of ‘self’ and adds a patch jobs to the task queue.

‘S==x’

JSONPatch.__eq__(x)[source]

Compares this pointer with x.

Args:
x: A valid Pointer.
Returns:
True or False
Raises:
JSONPointerException

‘S+=x’

JSONPatch.__iadd__(x=None)[source]

Adds patch jobs to the task queue in place.

‘S-=x’

JSONPatch.__isub__(x)[source]

Removes the patch job from the task queue in place.

Removes one of the following type(x) variants:

int: The patch job with given index.

JSONPatchItem: The first matching entry from
the task queue.
Args:
x: Item to be removed.
Returns:
Returns resulting list without x.
Raises:
JSONPatchException:

‘S!=x’

JSONPatch.__ne__(x)[source]

Compares this pointer with x.

Args:
x: A valid Pointer.
Returns:
True or False
Raises:
JSONPointerException

‘S-x’

JSONPatch.__sub__(x)[source]

Removes the patch job from the task queue.

Removes one of the following type(x) variants:

int: The patch job with given index.

JSONPatchItem: The first matching entry from
the task queue.
Args:
x: Item to be removed.
Returns:
Returns resulting list without x.
Raises:
JSONPatchException:

len

JSONPatch.__len__()[source]

The number of outstanding patches.

Iterators

__iter__

JSONPatch.__iter__()[source]

Provides an iterator foreseen for large amounts of in-memory patches.

JSONPatchItem

class jsondata.JSONPatch.JSONPatchItem(op, target, param=None)[source]

Record entry for list of patch tasks.

Attributes:
op: operations:
add, copy, move, remove, replace, test

target: JSONPointer for the modification target, see RFC6902.

value: Value, either a branch, or a leaf of the JSON data structure. src: JSONPointer for the modification source, see RFC6902.

Methods

__init__

JSONPatchItem.__init__(op, target, param=None)[source]

Create an entry for the patch list.

Args:

op: Operation: add, copy, move, remove, replace, test

target: Target node.

param: Parameter specific for the operation:
value: add,replace, test src: copy, move param:=None for ‘remove’
Returns:
When successful returns ‘True’, else returns either ‘False’, or raises an exception. Success is the complete addition only, thus one failure returns False.
Raises:
JSONDataSerializerError:

__repr__

JSONPatchItem.__repr__()[source]

Prints the patch string in accordance to RFC6901.

__str__

JSONPatchItem.__str__()[source]

Prints the patch string in accordance to RFC6901.

apply

JSONPatchItem.apply(jsondata)[source]

Applies the present patch list on the provided JSON document.

Args:
jsondata: Document to be patched.
Returns:

When successful returns ‘True’, else raises an exception. Or returns a tuple:

(n,lerr): n: number of present active entries
lerr: list of failed entries
Raises:
JSONPatchException:

repr_export

JSONPatchItem.repr_export()[source]

Prints the patch string for export in accordance to RFC6901.

Operators

‘()’

JSONPatchItem.__call__(j)[source]

Evaluates the related task for the provided data.

Args:
j: JSON data the task has to be
applied on.
Returns:
Returns a tuple of:

0: len of the job list 1: list of the execution status for

the tasks
Raises:
JSONPatchException:

‘[]’

JSONPatchItem.__getitem__(key)[source]

Support of various mappings.

  1. self[key]
  2. self[i:j:k]
  3. x in self
  4. for x in self

‘S==x’

JSONPatchItem.__eq__(x)[source]

Compares this pointer with x.

Args:
x: A valid Pointer.
Returns:
True or False
Raises:
JSONPointerException

‘S!=x’

JSONPatchItem.__ne__(x)[source]

Compares this pointer with x.

Args:
x: A valid Pointer.
Returns:
True or False
Raises:
JSONPointerException

JSONPatchItemRaw

class jsondata.JSONPatch.JSONPatchItemRaw(patchstring)[source]

Adds native patch strings or an unsorted dict for RFC6902.

Methods

__init__

JSONPatchItemRaw.__init__(patchstring)[source]

Parse a raw patch string in accordance to RFC6902.

Class: JSONPatchFilter

class jsondata.JSONPatch.JSONPatchFilter(**kargs)[source]

Filtering capabilities on the entries of patch lists.

Methods

__init__

JSONPatchFilter.__init__(**kargs)[source]
Args:
**kargs: Filter parameters:

Common:

contain=(True|False): Contain, else equal.

type=<node-type>: Node is of type.

Paths:

branch=<branch>:

deep=(): Determines the depth of comparison.

prefix=<prefix>: Any node of prefix. If prefix is
absolute: the only and one, else None. relative: any node prefixed by the path fragment.
Values:
val=<node-value>: Node ha the value.
Returns:
True or False
Raises:
JSONPointerException:

Operators

‘==’

JSONPatchFilter.__eq__(x)[source]

‘!=’

JSONPatchFilter.__ne__(x)[source]