[Python-ideas] JSON encoding protocol with __json__ dunder method

Steven D'Aprano steve at pearwood.info
Thu Feb 1 18:26:36 EST 2018


On Thu, Feb 01, 2018 at 08:45:13PM +0000, Kiss, György wrote:
> Hi!
> 
> 
> Most of the classes (even if very simple like datetime.datetime) cannot be
> serialized to JSON by default.
> 
> Would it be a good idea for the default json.JSONEncoder to call the
> __json__ dunder method automatically if the object has one?
> I can't find anything about why this protocol or PEP doesn't exists yet.


See: http://bugs.python.org/issue27362

This has been discussed before:

https://mail.python.org/pipermail/python-ideas/2010-July/007811.html

https://mail.python.org/pipermail/python-ideas/2011-March/009644.html


Please familiarise yourself with the objections and counter-arguments 
already discussed before trying to debate this again.



-- 
Steve


More information about the Python-ideas mailing list