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

2020-05-15 Thread Haoyu SUN


New submission from Haoyu SUN :

Float numbers in Python can have 3 special number: nan, inf, -inf, which are 
encoded by json module as "NaN", "Infinity", "-Infinity". These representations 
are not compatible with JSON specifications RFC7159:
https://tools.ietf.org/html/rfc7159.html#page-6

These values are not correctly parsed by most JavaScript JSON encoders.

It is better to encode "NaN" to "null" which is a valid JSON keyword 
representing "Not a Number".

Here is an example how json.dumps() encodes NaN to NaN in JSON:
Python 3.6.9 (default, Apr 18 2020, 01:56:04)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> dct = {'a': None, 'b' : float('nan')}
>>> dct
{'a': None, 'b': nan}
>>> import json
>>> json.dumps(dct)
'{"a": null, "b": NaN}'

--
components: Library (Lib)
messages: 368942
nosy: Haoyu SUN
priority: normal
severity: normal
status: open
title: json.dumps() should encode float number NaN to null
type: behavior
versions: Python 3.6

___
Python tracker 
<https://bugs.python.org/issue40633>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2020-05-15 Thread Haoyu SUN


Haoyu SUN  added the comment:

Thank you for the timely reply, Eric.

How about we add an optional argument (like the argument "ignore_nan" defaults 
to False as the package simplejson does) to functions like json.dumps(). So 
that user can choose whether he needs NaN encoded as NaN or null, meanwhile the 
default behavior stays the same.

In chromium based browsers, the function JSON.parse cannot parse it correctly. 
Here is an example below:
> JSON.parse('{"a": null, "b": NaN}')
uncaught SyntaxError: Unexpected token N in JSON at position 17
at JSON.parse ()
at :1:6

--

___
Python tracker 
<https://bugs.python.org/issue40633>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2020-05-16 Thread Haoyu SUN

Haoyu SUN  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 
<https://bugs.python.org/issue40633>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com