Skip to content

JSON Types

This module contains the strict JSON helper types used throughout JsonPatchX contracts and operation models.

JSONScalar = JSONBoolean | JSONNumber | JSONString | JSONNull

Strict JSON scalar helper union.

JSONContainer = JSONArray[T] | JSONObject[T]

Strict JSON container helper union.

JSONBound = JSONScalar | Sequence[JSONBound] | Mapping[str, JSONBound]

Bound for recursively JSON-shaped values accepted by generic helpers such as JSONPointer[T].

JSONBoolean

Strict JSON boolean helper used in Pydantic-backed patch contracts.

Source code in jsonpatchx/types.py
class JSONBoolean:
    """Strict JSON boolean helper used in Pydantic-backed patch contracts."""

    @classmethod
    def __get_pydantic_core_schema__(
        cls, _source_type: object, _handler: core_schema.GetCoreSchemaHandler
    ) -> core_schema.CoreSchema:
        return _strict_validator(Annotated[bool, Field(strict=True)])

    @classmethod
    def __get_pydantic_json_schema__(
        cls,
        _core_schema: core_schema.CoreSchema,
        _handler: core_schema.GetJsonSchemaHandler,
    ) -> dict[str, object]:
        return {"type": "boolean"}

JSONNumber

Strict JSON number helper accepting int or finite float values.

Source code in jsonpatchx/types.py
class JSONNumber:
    """Strict JSON number helper accepting `int` or finite `float` values."""

    @classmethod
    def __get_pydantic_core_schema__(
        cls, _source_type: object, _handler: core_schema.GetCoreSchemaHandler
    ) -> core_schema.CoreSchema:
        type _JSONNumberInternal = Annotated[  # NOTE: document the necessity of field strictness. adapters strict too for preventing "2" -> 2 for JSONBoolean and int/float
            Annotated[int, Field(strict=True)]
            | Annotated[float, Field(strict=True, allow_inf_nan=False)],
            Field(
                description="integer or finite float (no NaN/Infinity).",
            ),
        ]
        return _strict_validator(_JSONNumberInternal)

    @classmethod
    def __get_pydantic_json_schema__(
        cls,
        _core_schema: core_schema.CoreSchema,
        _handler: core_schema.GetJsonSchemaHandler,
    ) -> dict[str, object]:
        return {"type": "number"}

JSONString

Strict JSON string helper used in operation models and patch schemas.

Source code in jsonpatchx/types.py
class JSONString:
    """Strict JSON string helper used in operation models and patch schemas."""

    @classmethod
    def __get_pydantic_core_schema__(
        cls, _source_type: object, _handler: core_schema.GetCoreSchemaHandler
    ) -> core_schema.CoreSchema:
        return _strict_validator(Annotated[str, Field(strict=True)])

    @classmethod
    def __get_pydantic_json_schema__(
        cls,
        _core_schema: core_schema.CoreSchema,
        _handler: core_schema.GetJsonSchemaHandler,
    ) -> dict[str, object]:
        return {"type": "string"}

JSONNull

Strict JSON null helper.

Source code in jsonpatchx/types.py
class JSONNull:
    """Strict JSON null helper."""

    @classmethod
    def __get_pydantic_core_schema__(
        cls, _source_type: object, _handler: core_schema.GetCoreSchemaHandler
    ) -> core_schema.CoreSchema:
        return _strict_validator(Annotated[None, Field()])

    @classmethod
    def __get_pydantic_json_schema__(
        cls,
        _core_schema: core_schema.CoreSchema,
        _handler: core_schema.GetJsonSchemaHandler,
    ) -> dict[str, object]:
        return {"type": "null"}

JSONArray

Strict JSON array helper restricted to concrete list values.

Source code in jsonpatchx/types.py
class JSONArray[T]:
    """Strict JSON array helper restricted to concrete `list` values."""

    @classmethod
    def __get_pydantic_core_schema__(
        cls, source_type: object, handler: core_schema.GetCoreSchemaHandler
    ) -> core_schema.CoreSchema:
        (item_type,) = get_args(source_type) or (Any,)
        item_schema = handler.generate_schema(item_type)
        return core_schema.list_schema(item_schema, strict=True)

    @classmethod
    def __get_pydantic_json_schema__(
        cls,
        _core_schema: core_schema.CoreSchema,
        handler: core_schema.GetJsonSchemaHandler,
    ) -> dict[str, object]:
        return handler(_core_schema)

JSONObject

Strict JSON object helper restricted to dict[str, ...] values.

Source code in jsonpatchx/types.py
class JSONObject[T]:
    """Strict JSON object helper restricted to `dict[str, ...]` values."""

    @classmethod
    def __get_pydantic_core_schema__(
        cls, source_type: object, handler: core_schema.GetCoreSchemaHandler
    ) -> core_schema.CoreSchema:
        (value_type,) = get_args(source_type) or (Any,)
        value_schema = handler.generate_schema(value_type)
        return core_schema.dict_schema(
            core_schema.str_schema(), value_schema, strict=True
        )

    @classmethod
    def __get_pydantic_json_schema__(
        cls,
        _core_schema: core_schema.CoreSchema,
        handler: core_schema.GetJsonSchemaHandler,
    ) -> dict[str, object]:
        return handler(_core_schema)

JSONValue

Runtime JSON value type with strict validation and minimal OpenAPI schema.

Validation delegates to the strict JSON union, while JSON schema is deliberately inlined as {} to avoid a named component.

Source code in jsonpatchx/types.py
class JSONValue:
    """
    Runtime JSON value type with strict validation and minimal OpenAPI schema.

    Validation delegates to the strict JSON union, while
    JSON schema is deliberately inlined as `{}` to avoid a named component.
    """

    @classmethod
    def __get_pydantic_core_schema__(
        cls, _source_type: object, _handler: core_schema.GetCoreSchemaHandler
    ) -> core_schema.CoreSchema:
        type _JSONValueInternal = Annotated[
            JSONBoolean
            | JSONNumber
            | JSONString
            | JSONNull
            | JSONArray[_JSONValueInternal]
            | JSONObject[_JSONValueInternal],
            Field(),
        ]
        return _strict_validator(_JSONValueInternal)

    @classmethod
    def __get_pydantic_json_schema__(
        cls,
        _core_schema: core_schema.CoreSchema,
        _handler: core_schema.GetJsonSchemaHandler,
    ) -> dict[str, object]:
        return {}