On Fri, Aug 9, 2019 at 6:31 AM Richard Musil <[email protected]> wrote:
>
> Chris Angelico wrote:
>
> > 2) Should there be a protocol obj.__json__() to return a string
> > representation of an object for direct insertion into a JSON file?
>
> > However, this is a much broader topic, and if you want to push for
> > that, I would recommend starting a new thread. As Andrew pointed out,
> > trying to get bit-for-bit identical JSON representations out of
> > different encoders is usually a bad idea.
>
> I am not sure I have ever asked for bit-for-bit identical JSON 
> representation. I have always only mentioned `decimal.Decimal` and the lack 
> of proper way to encode it (while having the proper way of decoding it), and 
> if you read the subject of the OP it is asking for "raw output" (in the 
> encoder, nothing about underlying representation) which if I understand your 
> two options basically corresponds to the second one and is probably addressed 
> elsewhere far more thoroughly.
>

If you're checking hashes, you need it to be bit-for-bit identical.
But if what you REALLY want is for a Decimal to be represented in JSON
as a native number, then I think that is a very reasonable feature
request. The question is, how should it be done? And there are
multiple viable options.

I'd recommend, rather than requesting a way to create raw output, that
you request a way to either (a) recognize Decimals in the default JSON
encoder, both the Python and C implementations thereof; or (b) create
a protocol such as __json__ to allow any object to choose how it
represents itself. You'll probably find a reasonable level of support
for either of those suggestions.

ChrisA
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/4KXUIVHWGY3MIB32ZLKFZQXNDU65OV34/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to