[issue40633] json.dumps() should encode float number NaN to null

Haoyu SUN report at bugs.python.org
Sat May 16 09:13:35 EDT 2020


Haoyu SUN <raptorsun at gmail.com> added the comment:

About using null in JSON to represnet NaN value of a float type, I prefer this logic: float is a numeric type that expecting a number as its value, "Not a Number" on a numeric type is equivalent to None (¬Number ∩ NumericValues = Empty). If we need to capture an error in calculation or input data, we can use the allow_nan option to catch it. Database connectors such as SQLAlchemy translate an empty field as float('nan') for a float number field. Probably we can safely take it as a convention. No idea yet for representing infinity.

Once encoded, there is no way to know a null originates from NaN or None without additional fields.

The direct conversion from Python data types to JSON may lose part of information due to JSON's limited data types. When converting a BMP image to GIF, we have to eliminate some colors to fit in the small pallet and we do not expect to restore the full information BMP image has from its GIF counterpart.

I suggest we make the JSON module have at least an option to generate 
 standard-compliant JSON regardless potential loss of information, instead of leaving each application to have its subclass of JSONEncoder just for this corner case.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40633>
_______________________________________


More information about the Python-bugs-list mailing list