@@ -243,6 +243,11 @@ Basic Usage
243243 .. versionchanged :: 3.6
244244 All optional parameters are now :ref: `keyword-only <keyword-only_parameter >`.
245245
246+ .. versionchanged :: next
247+ Serialization can now be customized per type
248+ with the :meth: `~object.__json__ ` method and :func: `copyreg.json `.
249+ See :ref: `json-protocol `.
250+
246251
247252.. function :: dumps(obj, *, skipkeys=False, ensure_ascii=True, \
248253 check_circular=True, allow_nan=True, cls=None, \
@@ -560,6 +565,14 @@ Encoders and Decoders
560565 .. versionchanged :: 3.6
561566 All parameters are now :ref: `keyword-only <keyword-only_parameter >`.
562567
568+ .. versionchanged :: next
569+ The encoder now consults the dispatch table
570+ (:attr: `~JSONEncoder.dispatch_table `
571+ or :data: `copyreg.json_dispatch_table `)
572+ and the :meth: `~object.__json__ ` method of the object's class
573+ before falling back to *default *.
574+ See :ref: `json-protocol `.
575+
563576
564577 .. method :: default(o)
565578
@@ -598,6 +611,106 @@ Encoders and Decoders
598611 for chunk in json.JSONEncoder().iterencode(bigobject):
599612 mysocket.write(chunk)
600613
614+ .. attribute :: dispatch_table
615+
616+ A mapping of types to serialization functions,
617+ used instead of the global :data: `copyreg.json_dispatch_table `.
618+ It can be set as a class attribute of a subclass
619+ or as an attribute of an encoder instance.
620+ See :ref: `json-protocol `.
621+
622+ .. versionadded :: next
623+
624+
625+ .. _json-protocol :
626+
627+ Serializing custom objects
628+ --------------------------
629+
630+ Serialization of objects of types that are not supported natively
631+ (see the :ref: `conversion table <py-to-json-table >`)
632+ can be customized at three levels:
633+
634+ * The author of a class can define the JSON representation of its instances
635+ by giving it a :meth: `~object.__json__ ` method.
636+
637+ * An application can register a serialization function
638+ for instances of a type it does not control
639+ with :func: `copyreg.json `.
640+
641+ * A particular call can use its own type-to-function mapping
642+ by setting the :attr: `~JSONEncoder.dispatch_table ` attribute
643+ of a :class: `JSONEncoder ` subclass or instance.
644+
645+ The more specific level takes precedence:
646+ a registered function is used before ``__json__ ``,
647+ and an encoder's own dispatch table completely replaces
648+ the global :data: `copyreg.json_dispatch_table `.
649+ The *default * function is called last,
650+ only for objects that none of the above handled.
651+
652+ .. currentmodule :: None
653+
654+ .. method :: object.__json__()
655+
656+ Return a substitute object to be serialized instead of *self *.
657+ Called by the JSON encoder for an object
658+ whose type is not supported natively
659+ and has no entry in the dispatch table.
660+
661+ If the result is of a serializable type, it is serialized as usual
662+ (but a ``__json__ `` method of the result is not consulted).
663+ Otherwise the result is interpreted by duck typing:
664+
665+ * an object with :meth: `~object.__index__ ` is serialized
666+ as a JSON number;
667+ * an object with :meth: `~object.__float__ ` is serialized
668+ as a JSON number;
669+ * an iterable with a :meth: `!keys ` method and
670+ :meth: `~object.__getitem__ ` is serialized as a JSON object;
671+ * any other iterable is serialized as a JSON array;
672+ * an object with :meth: `~object.__raw_json__ ` is included
673+ in the output verbatim.
674+
675+ .. versionadded :: next
676+
677+ .. method :: object.__raw_json__()
678+
679+ Return a string to be included in the JSON output verbatim,
680+ without validation of its content.
681+ Consulted only for the result of a registered serialization function
682+ or a :meth: `~object.__json__ ` method.
683+ To include an already encoded fragment directly in the serialized data,
684+ wrap it in :class: `copyreg.RawJSON ` instead.
685+
686+ .. versionadded :: next
687+
688+ .. currentmodule :: json
689+
690+ For example, a class can serialize itself as a JSON object::
691+
692+ class Point:
693+ def __init__(self, x, y):
694+ self.x = x
695+ self.y = y
696+
697+ def __json__(self):
698+ return {'x': self.x, 'y': self.y}
699+
700+ An application can choose how :class: `decimal.Decimal ` is serialized —
701+ as a JSON string::
702+
703+ copyreg.json(decimal.Decimal, str)
704+
705+ or as a JSON number with full precision, using :class: `copyreg.RawJSON `::
706+
707+ copyreg.json(decimal.Decimal, lambda d: copyreg.RawJSON(str(d)))
708+
709+ A number of standard library container types define
710+ :meth: `~object.__json__ ` and are therefore serializable out of the box.
711+
712+ .. versionadded :: next
713+
601714
602715Exceptions
603716----------
0 commit comments