Actually-unambiguous parsing of RFC 6901 JSON Pointers in Python
Project description
jsonpointerparse
Actually-unambiguous parsing of RFC 6901 JSON Pointers in Python
pip install jsonpointerparse
Unlike implementations such as stefankoegl/python-json-pointer,
jsonpointerparse satisfies RFC 6901's entire spec without actually evaluating, accessing, or having knowledge of
a target document. Consequently, its source code is tiny, and can be read faster than the documentation. There are no
dependencies.
Serving as a utility or a library primitive, jsonpointerparse is designed to make it easier to write robust,
bug-free implementations of evaluation and manipulation of JSON structures targeted by a JSON pointer. It doesn't
care whether you're extending JSON Patch, another standard, or
building a new one.
Basic Usage
from jsonpointerparse import JsonPointer
pointer = JsonPointer.from_string('/items/100') # JsonPointer(parts=('items', 100))
print(pointer.parts) # ('items', 100)
Documentation
JsonPointerPart
type JsonPointerPart = str | int | AfterEndOfArray
Each part in JsonPointer().parts is a valid, unescaped, type-converted reference token
of a JSON Pointer.
A part's type lets you know if and how the part could be used in array operations on a future document.
int: A part that could represent an array index if the targeted parent were an array.- Found if: Raw token is
0, or just digits with no leading zeros.
- Found if: Raw token is
AfterEndOfArray: A part that could represent a non-existent member after the last element of an array, if the targeted parent were an array.- Found if: Raw token is
-, and is the last part in the pointer.
- Found if: Raw token is
str: Any part that doesn't meet the criteria for the other types, and thus cannot be used for array manipulation. It's unescaped, so it may include normal/and~characters. (escaped versions are~1and~0, respectively)
Note:
AfterEndOfArrayis an empty singleton on whichstr()returns'-'. AnAFTER_END_OF_ARRAYconstant is also available with a reference to the singleton instance.
JsonPointer
@dataclass(frozen=True, slots=True)
class JsonPointer:
parts: tuple[JsonPointerPart, ...] = ()
A lightweight, immutable dataclass to represent a JSON Pointer as validated, pre-processed, type-converted parts, ready for document evaluation.
Has a minimal API: parts, from_string(cls), to_string(self), @property is_root
In addition to validating and unescaping the pointer, from_string()
converts parts to appropriate types for potential array operations without knowledge
of the target document.
>>> pointer = JsonPointer.from_string('/~01~1bar/10 /001/100/-/-')
>>> pointer.parts
('~1/bar', '10 ', '001', 100, '-', AfterEndOfArray())
Here are those parts, before and after parsing:
'~01~1bar' |
'~1/bar' |
Unescaped ~0 and ~1 |
'10 ' |
'10 ' |
No change |
'001' |
'001' |
No change |
'100' |
100 |
Type change. Can be used as array index. |
'-' |
'-' |
No change |
'-' |
AfterEndOfArray() |
Type change. Can represent new array member. |
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file jsonpointerparse-0.1.0.tar.gz.
File metadata
- Download URL: jsonpointerparse-0.1.0.tar.gz
- Upload date:
- Size: 19.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
57f7c8fd2e64d2118130061a3f3b9966e5ad80d189cc307dc43df76464d64bcb
|
|
| MD5 |
527acaf7f275139961cb96316b5f4571
|
|
| BLAKE2b-256 |
bc5bedf75eb3b105509df721fdbac4ec166ea9d6d1125dbab090aa5c8fbf5bf3
|
File details
Details for the file jsonpointerparse-0.1.0-py3-none-any.whl.
File metadata
- Download URL: jsonpointerparse-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
150cbc64a3c02244fc70e76cf30e29809f53caec56c1a9512580626ea5f89f20
|
|
| MD5 |
7706dc8f998df22604d3b89b91e3667e
|
|
| BLAKE2b-256 |
63dc978154d3219e7bc8a018d07a4dd0b065d759daab89315c7ef6c37c3f75e6
|