On Mon, Aug 12, 2019 at 3:01 PM Christopher Barker <python...@gmail.com> wrote: > > On Sun, Aug 11, 2019 at 5:12 PM David Shawley <daveshaw...@gmail.com> wrote: >> >> 1) Should JSONEncoder (the class underlying json.dumps) natively >> support decimal.Decimal, and if so, can it avoid importing that module >> unnecessarily? > > I think yes on both counts :-) >> >> 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 think if we ARE going to extend json to allow more flexibility, yes, a > protocol would be a good way to do it. > > But it makes me nervous -- I think the goal of the json module is to produce > valid json, and nothing else. Opening up a protocol would allow users to > fairly easily, and maybe inadvertently, create invalid JSON. I'm not sure > there are compelling use cases that make that worth it. >
You can already make invalid JSON, and far more easily. >>> json.dumps({"spam": [1,2,3]}, separators=(' : ',', ')) '{"spam", [1 : 2 : 3]}' It's not the module's job to stop you from shooting yourself in the foot. ChrisA _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/JSG3EKNSCWUHAUOHUMQSEMKMV2JTXWAH/ Code of Conduct: http://python.org/psf/codeofconduct/