On Aug 8, 2019, at 3:55 PM, Chris Angelico <rosuav@gmail.com> wrote:


There are two broad suggestions that came out of that thread and
others, and I think it may be worth reopening them.

1) Should JSONEncoder (the class underlying json.dumps) natively
support decimal.Decimal, and if so, can it avoid importing that module
unnecessarily?

2) Should there be a protocol obj.__json__() to return a string
representation of an object for direct insertion into a JSON file?

I'm inclined towards the protocol, since there are protocols for
various other encoders (eg deepcopy, pickle), and it avoids the
problem of json importing decimal. It can also be implemented entirely
as a third-party patch, although you'd need to subclass Decimal to add
that method.


I proposed something similar about a year ago [1].  I really like the idea
of a protocol for this.  Especially since the other encoders already use
this approach.  Should I reboot this approach?  The implementation was
really simple [2].

- dave

[1]: https://mail.python.org/archives/list/python-ideas@python.org/thread/ZC4OOAVDA5ZFNCSFKDNO7P3UUOXO3DCG/
[2]: https://github.com/dave-shawley/cpython/pull/2