jschon.jsonpatch

class jschon.jsonpatch.JSONPatch(*operations)

RFC 6902-conformant JSON Patch implementation.

__delitem__(index)

Delete self[index].

Parameters

index (int) –

Return type

None

__eq__(other)

Return self == other.

Parameters

other (Union[jschon.jsonpatch.JSONPatch, Iterable[Union[jschon.jsonpatch.JSONPatchOperation, Mapping[str, Union[None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]]]]]) –

Return type

bool

__getitem__(index: int) jschon.jsonpatch.JSONPatchOperation
__getitem__(index: slice) jschon.jsonpatch.JSONPatch

Return self[index].

__init__(*operations)

Initialize a JSONPatch instance from the given operations, each of which may be a JSONPatchOperation or a JSON patch operation-conformant dictionary.

Parameters

operations (Union[jschon.jsonpatch.JSONPatchOperation, Mapping[str, Union[None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]]]) –

Return type

None

__len__()

Return len(self).

Return type

int

__repr__()

Return repr(self).

Return type

str

__setitem__(index, operation)

Set self[index] to operation.

Parameters
Return type

None

aslist()

Return self as a list of operation dicts.

Return type

List[Dict[str, Union[None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]]]

evaluate(document)

Return the result of sequentially applying all patch operations to document, as a new document. document itself is not modified.

Parameters

document (Union[None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]) –

Return type

Union[None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]

insert(index, operation)

Insert operation before index.

Parameters
Return type

None

class jschon.jsonpatch.JSONPatchOperation(*, op, path, value=None, from_=None, **kwargs)

RFC 6902-conformant JSON patch operation object.

Parameters
Return type

JSONPatchOperation

__eq__(other)

Return self == other.

Parameters

other (Union[jschon.jsonpatch.JSONPatchOperation, Mapping[str, Union[None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]]]) –

Return type

bool

static __new__(cls, *, op, path, value=None, from_=None, **kwargs)

Create and return a new JSONPatchOperation instance.

Parameters
  • op (jschon.jsonpatch.PatchOp) – The operation to perform. One of add, remove, replace, move, copy, test.

  • path (Union[str, jschon.jsonpointer.JSONPointer]) – A JSON pointer to the target location.

  • value (Union[None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]) – For add and replace operations, the value to set at the target location. For test, the value to compare with the target.

  • from – The location from which to move or copy. An alias for from, which may be passed via kwargs.

  • from_ (Optional[Union[str, jschon.jsonpointer.JSONPointer]]) –

  • kwargs (Union[str, jschon.jsonpointer.JSONPointer]) –

Return type

jschon.jsonpatch.JSONPatchOperation

__repr__()

Return repr(self).

Return type

str

apply(document)

Apply the patch operation to document and return the resultant document.

Parameters

document (Union[None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]) –

Return type

Union[None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]

asdict()

Return self as a dict.

Return type

Dict[str, Union[None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]]

class jschon.jsonpatch.PatchOp(value)

An enumeration.

ADD = 'add'
COPY = 'copy'
MOVE = 'move'
REMOVE = 'remove'
REPLACE = 'replace'
TEST = 'test'
jschon.jsonpatch.add(document, path, value)

Add value to document at path.

Parameters
  • document (Union[None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]) –

  • path (jschon.jsonpointer.JSONPointer) –

  • value (Union[None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]) –

Return type

Union[None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]

jschon.jsonpatch.copy(document, path, from_)

Copy the value at from_ in document to path.

Parameters
Return type

Union[None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]

jschon.jsonpatch.move(document, path, from_)

Move the value at from_ in document to path.

Parameters
Return type

Union[None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]

jschon.jsonpatch.remove(document, path)

Remove the value at path in document.

Parameters
Return type

Union[None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]

jschon.jsonpatch.replace(document, path, value)

Replace the value at path in document with value.

Parameters
  • document (Union[None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]) –

  • path (jschon.jsonpointer.JSONPointer) –

  • value (Union[None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]) –

Return type

Union[None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]

jschon.jsonpatch.test(document, path, value)

Test whether the value at path in document is equal to value.

Parameters
  • document (Union[None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]) –

  • path (jschon.jsonpointer.JSONPointer) –

  • value (Union[None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]) –

Return type

Union[None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]