Skip to content

pydantic_jsonpointer.types

pydantic_jsonpointer.types

JsonPointer

Bases: str

RFC 6901 JSON Pointer.

A JSON Pointer is either the empty string (referencing the whole document) or a sequence of reference tokens each prefixed by /.

Construct from a raw string, from individual tokens, or by chaining the / operator::

ptr = JsonPointer("/foo/0")
ptr = JsonPointer.from_tokens("foo", 0)
ptr = JsonPointer() / "foo" / 0

tokens property

tokens: tuple[str, ...]

Decompose into unescaped reference tokens.

from_tokens classmethod

from_tokens(*tokens: str | int) -> Self

Build a pointer from unescaped reference tokens.

Tokens are escaped per RFC 6901 (~ -> ~0, / -> ~1).

Source code in src/pydantic_jsonpointer/types.py
@classmethod
def from_tokens(cls, *tokens: str | int) -> Self:
    """Build a pointer from unescaped reference tokens.

    Tokens are escaped per RFC 6901 (``~`` -> ``~0``, ``/`` -> ``~1``).
    """
    if not tokens:
        return cls.__with_tokens("", ())
    str_tokens = tuple(str(t) for t in tokens)
    value = "/" + "/".join(_escape_token(t) for t in str_tokens)
    return cls.__with_tokens(value, str_tokens)