gh-71549: Add support for JSON serialization of custom types#153607
Draft
serhiy-storchaka wants to merge 3 commits into
Draft
gh-71549: Add support for JSON serialization of custom types#153607serhiy-storchaka wants to merge 3 commits into
serhiy-storchaka wants to merge 3 commits into
Conversation
Add the __json__() and __raw_json__() protocol methods, the copyreg.json() registration function with copyreg.json_dispatch_table, the copyreg.RawJSON wrapper for including already encoded JSON in the output verbatim, and the JSONEncoder.dispatch_table attribute for overriding the global registry per encoder. Make deque, mappingproxy, ChainMap, UserDict, UserList and UserString serializable out of the box. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Documentation build overview
12 files changed ·
|
a955cf2 to
82fbf68
Compare
… docs build Reset the module context around the object.__json__() and object.__raw_json__() directives so that they are registered outside the json namespace, like the dunders in reference/datamodel.rst, and :meth:`~object.__json__` references from other documents resolve. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
82fbf68 to
010620b
Compare
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements PEP 837: JSON serialization of custom types can be
customized at three levels.
A class defines the JSON representation of its instances with the
new
__json__()method.An application registers a serialization function for a type it does
not control with the new
copyreg.json(). The newcopyreg.RawJSONwrapper includes an already encoded fragment inthe output verbatim:
A particular encoder overrides the global registry with the new
JSONEncoder.dispatch_tableattribute, mirroringpickle.Pickler.dispatch_table.default=is unchanged andruns last.
deque,mappingproxy,ChainMap,UserDict,UserListand
UserStringserialize out of the box.Both encoder implementations are updated with identical semantics, and
the
jsonmodule gains no new public names. Tests (bothimplementations, refleak-clean), docs, NEWS and What's New are
included.
Besides gh-71549, this directly resolves gh-64973, gh-73849 (deque) and
gh-79039 (mappingproxy), and provides the mechanism requested in
gh-79292, gh-114285, gh-86957 (raw output), gh-67312, gh-118810,
gh-145115 (Decimal), gh-70451 (array), gh-67661 (namedtuple), gh-65742
(datetime), gh-75338, gh-78031, gh-83377 (iterables, generators, dict
views) and gh-81022, gh-98306, gh-134717 (float formatting).
PEP: python/peps#5031 (PEP 837, draft)
Discussion: https://discuss.python.org/t/pep-837-extensible-json-serialization/108124
Closes gh-71549.
🤖 Generated with Claude Code