PEP 837 proposes extensible JSON serialization for Python
Python's standard library may soon gain an extensible JSON serialization mechanism, addressing long-standing limitations in handling custom types and complex representations. This PEP introduces a three-tiered approach: a library-level protocol (__json__), an application-level global registry, and a call-level dispatch table, allowing types to opt-in and applications to define serialization policies. It aims to improve interoperability and resolve issues with serializing types like Decimal, namedtuples, and datetimes, impacting developers who work with custom data structures or require precise JSON output.
- →Extensible JSON serialization protocol introduced
- →Support for serializing common standard library types added
- →Wrapper class for verbatim JSON string emission
- →Addresses limitations of current JSON serialization methods
- →Resolves ambiguity in standard type representations
Features (2) ›
- Extensible JSON serialization protocol introduced
PEP 837 proposes a three-part extension mechanism for Python's `json` encoder: a `__json__()` and `__raw_json__()` protocol for library-level customization, a global registry via `copyreg.json()` for application-level control, and a per-encoder dispatch table. This system allows for hierarchical customization, with more specific levels taking precedence.
- Support for serializing common standard library types added
Several standard library container types, including `collections.deque`, `types.MappingProxyType`, `collections.ChainMap`, `collections.UserDict`, `collections.UserList`, and `collections.UserString`, will gain a `__json__` method, enabling them to serialize out-of-the-box.
Enhancements (1) ›
- Wrapper class for verbatim JSON string emission
A helper class `copyreg.RawJSON` is introduced to wrap already-encoded JSON strings. This allows these strings to be emitted verbatim, facilitating representations not otherwise expressible, such as serializing `decimal.Decimal` with full precision as a JSON number.
Notes (3) ›
- Addresses limitations of current JSON serialization methods
The proposed mechanism aims to overcome limitations of the current `default=` function and `JSONEncoder` subclassing. It addresses issues with non-composable defaults, lack of opt-in for third-party types, and the impossibility of certain representations like pre-encoded JSON fragments or precise decimal numbers.
- Resolves ambiguity in standard type representations
The PEP acknowledges ambiguity in representing types like `Decimal`, `namedtuple`, `array.array`, and `datetime` in JSON. The proposed system allows applications to declare their specific serialization policies, providing a standardized way to handle these cases.
- Standard library adopts `__json__` protocol
This PEP formalizes the `__json__` protocol, which has seen piecemeal adoption in various libraries and frameworks. By defining it in the standard library, it provides a consistent and reliable mechanism for third-party libraries to rely on for JSON serialization.
https://peps.python.org/pep-0837/
Related releases
- OpenAI Agents Python v0.19.1: Bug fixes and documentation updates OpenAI Agents SDK (Python) Releases ·
- pydantic-ai v2.20.0: New Claude Opus, OpenAI Responses API, and Usage Fixes Pydantic AI Releases ·
- uv 0.12.0 improves dependency resolution, hash checking, and environment management uv Releases ·
- LangGraph 1.2.10: Stream events typing, trace policy changes LangGraph Releases ·
- FastAPI 0.140.13: Fixes SSE/JSONL status code, improves docs FastAPI Releases ·
- crewAI 1.15.8: WaitTool, FileWriter fix, and docs CrewAI Releases ·