Skip to content

Commit 763b6ff

Browse files
committed
feat: JSONPointer.parent
1 parent b7e4159 commit 763b6ff

3 files changed

Lines changed: 54 additions & 11 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
**Fixes**
66

7-
- Fixed a bug with the parsing of JSON Pointers. When given an arbitrary string without slashes, `JSONPointer` would resolve to the document root. The empty string is the only valid pointer that should resolve to the document root. See [#27](https://github.com/jg-rp/python-jsonpath/issues/27).
7+
- Fixed a bug with the parsing of JSON Pointers. When given an arbitrary string without slashes, `JSONPointer` would resolve to the document root. The empty string is the only valid pointer that should resolve to the document root. We now raise a `JSONPointerError` in such cases. See [#27](https://github.com/jg-rp/python-jsonpath/issues/27).
88

99
**Features**
1010

1111
- Added a command line interface, exposing JSONPath, JSON Pointer and JSON Patch features.
12+
- Added `JSONPointer.parent()`, a method that returns a the parent of the pointer, as a new `JSONPointer`.
1213

1314
## Version 0.8.1
1415

‎jsonpath/pointer.py‎

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def __init__(
6969
unicode_escape=unicode_escape,
7070
uri_decode=uri_decode,
7171
)
72-
self._s = pointer
72+
self._s = self._encode(self.parts)
7373

7474
def __str__(self) -> str:
7575
return self._s
@@ -215,7 +215,7 @@ def resolve_parent(
215215
JSONPointerTypeError: When attempting to resolve a non-index string
216216
path part against a sequence.
217217
"""
218-
if not len(self.parts):
218+
if not self.parts:
219219
return (None, self.resolve(data))
220220

221221
if isinstance(data, str):
@@ -232,20 +232,26 @@ def resolve_parent(
232232
except (JSONPointerIndexError, JSONPointerKeyError):
233233
return (parent, UNDEFINED)
234234

235+
@staticmethod
236+
def _encode(parts: Iterable[Union[int, str]]) -> str:
237+
if parts:
238+
return "/" + "/".join(
239+
str(p).replace("~", "~0").replace("/", "~1") for p in parts
240+
)
241+
return ""
242+
235243
@classmethod
236244
def from_match(
237245
cls,
238246
match: JSONPathMatch,
239247
) -> JSONPointer:
240248
"""Return a JSON Pointer for the path from a JSONPathMatch instance."""
241249
# A rfc6901 string representation of match.parts.
242-
if not match.parts:
250+
if match.parts:
251+
pointer = cls._encode(match.parts)
252+
else:
243253
# This should not happen, unless the JSONPathMatch has been tampered with.
244254
pointer = ""
245-
else:
246-
pointer = "/" + "/".join(
247-
str(p).replace("~", "~0").replace("/", "~1") for p in match.parts
248-
)
249255

250256
return cls(
251257
pointer,
@@ -285,9 +291,7 @@ def from_parts(
285291
__parts = tuple(_parts)
286292

287293
if __parts:
288-
pointer = "/" + "/".join(
289-
p.replace("~", "~0").replace("/", "~1") for p in __parts
290-
)
294+
pointer = cls._encode(__parts)
291295
else:
292296
pointer = ""
293297

@@ -308,6 +312,21 @@ def is_relative_to(self, other: JSONPointer) -> bool:
308312
def __eq__(self, other: object) -> bool:
309313
return isinstance(other, JSONPointer) and self.parts == other.parts
310314

315+
def parent(self) -> JSONPointer:
316+
"""Return this pointer's parent, as a new `JSONPointer`.
317+
318+
If this pointer points to the document root, self is returned.
319+
"""
320+
if not self.parts:
321+
return self
322+
parent_parts = self.parts[:-1]
323+
return JSONPointer(
324+
self._encode(parent_parts),
325+
parts=parent_parts,
326+
unicode_escape=False,
327+
uri_decode=False,
328+
)
329+
311330

312331
def resolve(
313332
pointer: Union[str, Iterable[Union[str, int]]],

‎tests/test_json_pointer.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,26 @@ def test_pointer_with_leading_whitespace() -> None:
219219
data = {"some": {"thing": [1, 2, 3]}}
220220
pointer = JSONPointer(" /some/thing/0")
221221
assert pointer.resolve(data) == 1
222+
assert str(pointer) == "/some/thing/0"
223+
224+
225+
def test_pointer_parent() -> None:
226+
data = {"some": {"thing": [1, 2, 3]}}
227+
pointer = JSONPointer("/some/thing/0")
228+
assert pointer.resolve(data) == 1
229+
230+
parent = pointer.parent()
231+
assert str(parent) == "/some/thing"
232+
assert parent.resolve(data) == [1, 2, 3]
233+
234+
parent = parent.parent()
235+
assert str(parent) == "/some"
236+
assert parent.resolve(data) == {"thing": [1, 2, 3]}
237+
238+
parent = parent.parent()
239+
assert str(parent) == ""
240+
assert parent.resolve(data) == {"some": {"thing": [1, 2, 3]}}
241+
242+
parent = parent.parent()
243+
assert str(parent) == ""
244+
assert parent.resolve(data) == {"some": {"thing": [1, 2, 3]}}

0 commit comments

Comments
 (0)