On Sun, 11 Aug 2019 20:09:41 -0400 David Shawley <daveshawley@gmail.com> wrote:
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].
I think this would be worthwhile. Here is a use case where it may remove some pain from users'life: https://bugs.python.org/issue24313 Regards Antoine.