Skip to content

Commit 34ae182

Browse files
committed
feat: JSON Patch
1 parent e1f9ca9 commit 34ae182

7 files changed

Lines changed: 606 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313

1414
**Features**
1515

16-
- Added the `JSONPointer` class and methods for converting a `JSONPathMatch` to a `JSONPointer`.
17-
- Added `jsonpath.pointer.resolve()`, a function for resolving a [JSON Pointer](https://datatracker.ietf.org/doc/html/rfc6901).
16+
- Added the `JSONPointer` class and methods for converting a `JSONPathMatch` to a `JSONPointer`. `JSONPointer` is compliant with [RFC 6901](https://datatracker.ietf.org/doc/html/rfc6901).
17+
- Added the `JSONPatch` class. `JSONPatch` implements [RFC 6902](https://datatracker.ietf.org/doc/html/rfc6902).
18+
- Added `jsonpath.pointer.resolve()`, a function for resolving a JSON Pointer.
1819
- Added `jsonpath.match()`, a convenience function returning a `JSONPathMatch` instance for the first match of a path, or `None` if there were no matches.
1920
- Added filter expression caching. Controlled with the `filter_caching` argument to `JSONPathEnvironment`, filter expression caching is enabled by default. See [#14]
2021
- All selectors now use `env.match_class` to instantiate new `JSONPathMatch` objects. This allows for subclassing of `JSONPathMatch`.

‎docs/syntax.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ This is a list of things that you might find in other JSONPath implementation th
181181
- We don't support extension functions of the form `selector.func()`.
182182
- We always return a list of matches from `jsonpath.findall()`, never a scalar value.
183183
- We do not support arithmetic in filter expression.
184-
- Python JSONPath is strictly read only. There are no update "selectors", although a Python API for working with `JSONPathMatch`s may well be added in the future.
184+
- Python JSONPath is strictly read only. There are no update "selectors", but we do provide methods for converting `JSONPathMatch` instances to `JSONPointer`s, and a `JSONPatch` builder API for modifying JSON-like data structures using said pointers.
185185

186186
And this is a list of areas where we deviate from the [IETF JSONPath draft](https://datatracker.ietf.org/doc/html/draft-ietf-jsonpath-base-13).
187187

‎jsonpath/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from .lex import Lexer
1818
from .match import JSONPathMatch
1919
from .parse import Parser
20+
from .patch import JSONPatch
2021
from .path import CompoundJSONPath
2122
from .path import JSONPath
2223
from .pointer import JSONPointer
@@ -29,6 +30,7 @@
2930
"findall",
3031
"finditer_async",
3132
"finditer",
33+
"JSONPatch",
3234
"JSONPath",
3335
"JSONPathEnvironment",
3436
"JSONPathError",

‎jsonpath/exceptions.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ class JSONPointerTypeError(JSONPointerResolutionError, TypeError):
100100
"""An exception raised when a pointer resolves a string against a sequence."""
101101

102102

103+
class JSONPatchError(Exception):
104+
"""Base class for all JSON Patch errors."""
105+
106+
107+
class JSONPatchTestFailure(JSONPatchError): # noqa: N818
108+
"""An exception raised when a JSON Patch _test_ op fails."""
109+
110+
103111
def _truncate_message(value: str, num: int, end: str = "...") -> str:
104112
if len(value) < num:
105113
return value

0 commit comments

Comments
 (0)