[New-bugs-announce] [issue40633] json.dumps() should encode float number NaN to null

Haoyu SUN report at bugs.python.org
Fri May 15 09:42:04 EDT 2020


New submission from Haoyu SUN <raptorsun at gmail.com>:

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 <report at bugs.python.org>
<https://bugs.python.org/issue40633>
_______________________________________


More information about the New-bugs-announce mailing list