Skip to main content

typed-json

stdlib json.loads/dumps, typed as a recursive JsonValue instead of Any.

json.loads is annotated -> Any, which silently switches off type checking for everything you do with the result. typed-json narrows that boundary to JsonValue — a recursive union (None | bool | int | float | str | list | dict) that the type checker can actually follow — so a single un-typed value can't quietly poison the types downstream of it.

from typed_json import loads, dumps

value = loads('{"items": [1, 2.5, null]}')   # JsonValue, not Any
text = dumps(value)                           # input constrained to JsonValue

The contract: no Any

That's the whole promise — nothing more, nothing less.

  • loads / load cast the stdlib result to JsonValue. Under the default decoder (no custom parse_* / object_hook settings) json.loads provably returns a value within JsonValue, so the cast is sound at zero runtime cost — no validation pass.
  • dumps / dump accept only JsonValue. Because that type is exactly the set of natively-serializable values, the checker rejects a set/datetime statically, and no default= hook is ever needed.

When stdlib's guarantee doesn't apply: the guards

For a value of genuinely unknown origin — an object/Any from another library, a YAML/msgpack/pickle load, hand-built data — cast is not sound. Reach for the TypeGuards, which validate at runtime by recursing the tree:

from typed_json import is_json_value

def handle(payload: object) -> None:
    if is_json_value(payload):
        reveal_type(payload)  # JsonValue

Also available: is_json_primitive, is_json_array, is_json_object.

Types

JsonValue, JsonPrimitive, JsonObject (dict[str, JsonValue]), and JsonArray (list[JsonValue]) are exported as PEP 695 type aliases.

Install

uv add python-typed-json   # or: pip install python-typed-json

The PyPI name carries a python- prefix because an unrelated, dormant typedjson project blocks the bare name; the module you import is plain typed_json, in the tradition of python-dateutil and python-dotenv.

Requires Python ≥ 3.12 (PEP 695 type statements). Zero runtime dependencies.

License

Apache-2.0

Release files for python-typed-json 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-typed-json 0.1.0
File Size Uploaded
python_typed_json-0.1.0.tar.gz 12.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-typed-json 0.1.0
File Interpreter ABI Platform
python_typed_json-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 20.6 kB

Release files / python_typed_json-0.1.0.tar.gz

Download URL python_typed_json-0.1.0.tar.gz
Size 12.6 kB
Tags Source
SHA-256 checksum
How to use checksums
b4c6a683ed8773113d91c33551788700b2785500799daeaad0b60d6d027892c6
BLAKE2b-256 checksum
How to use checksums
9839414bd3f01aa454ec1a580b5fb9bfd7ef4a32e84b45674fb7c0d43ef4ec28
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / python_typed_json-0.1.0-py3-none-any.whl

Download URL python_typed_json-0.1.0-py3-none-any.whl
Size 8.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cdae9a75b68a53ff64cacc219f11717ffc3f2b9dcec04f125606c0ab0265962c
BLAKE2b-256 checksum
How to use checksums
c1f887a969c50b7e1352d725771919ac9733581a9e6d17334f86e11f394a593b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page