‘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¶
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¶
apply¶
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:
Operators¶
‘()’¶
‘[]’¶
‘S+x’¶
‘S==x’¶
‘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’¶
‘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:
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:
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:
JSONPatchItemRaw¶
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: