@Richard I suggest you open a new thread to discuss support for dumping 
`Decimal` as a number in JSON.

I agree that allowing arbitrary strings to be inserted in the JSON is not a 
good idea, but apart from the `Decimal` issue I can't think of any other case 
that can't be solved via `JSONEncoder.decode`.

However there is an asymmetry with respect to parsing / dumping numbers. The 
JSON specs don't limit numbers to any precision and the `json` module can parse 
numbers of arbitrary precision. Python further supports arbitrary precision 
through `Decimal` but any JSON dump is limited to the underlying binary `float` 
representation. As the OP indicates one can parse `0.6441726684570313` to 
`Decimal` (preserving the precision) but there's not way to dump it back. This 
seems like a limitation of the `json` implementation.

For dumping `Decimal` without importing the `decimal` module, since this type 
seems to be the only exception, can't we just dump any non-default type as 
`str(o)` and check whether it can be parsed back to float:

    # if not any of the default types 
    dump_val = str(o)
    try:
        float(dump_val)
    except ValueError:
        raise TypeError('... is not JSON serializable') from None
_______________________________________________
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/3EHFAFM5CT7F42PQ35B7HZYJKA4KXVCF/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to