Skip to main content

Actually-unambiguous parsing of RFC 6901 JSON Pointers in Python

Project description

jsonpointerparse

PyPI Tests codecov License Black Pyright

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.
  • 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.
  • 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 ~1 and ~0, respectively)

Note: AfterEndOfArray is an empty singleton on which str() returns '-'. An AFTER_END_OF_ARRAY constant 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jsonpointerparse-0.1.0.tar.gz (19.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jsonpointerparse-0.1.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

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

Hashes for jsonpointerparse-0.1.0.tar.gz
Algorithm Hash digest
SHA256 57f7c8fd2e64d2118130061a3f3b9966e5ad80d189cc307dc43df76464d64bcb
MD5 527acaf7f275139961cb96316b5f4571
BLAKE2b-256 bc5bedf75eb3b105509df721fdbac4ec166ea9d6d1125dbab090aa5c8fbf5bf3

See more details on using hashes here.

File details

Details for the file jsonpointerparse-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for jsonpointerparse-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 150cbc64a3c02244fc70e76cf30e29809f53caec56c1a9512580626ea5f89f20
MD5 7706dc8f998df22604d3b89b91e3667e
BLAKE2b-256 63dc978154d3219e7bc8a018d07a4dd0b065d759daab89315c7ef6c37c3f75e6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page