← index #3511Issue #4780
Related · high · value 0.443
QUERY · ISSUE

ujson dumps NaN but doesn't load it

openby stinosopened 2017-12-21updated 2024-09-29
enhancement

The mod_ujson_load function doesn't handle nan though it is a valid representation:

>>> import ujson
>>> ujson.dumps(float('nan'))
'nan'
>>> ujson.loads(ujson.dumps(float('nan')))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: syntax error in JSON

Quick hack to solve this (sorry couldn't immediately figure out how to get the nan constant), line 115 in modujson.c:

                } else if (S_CUR(s) == 'a' && S_NEXT(s) == 'n') {
                    S_NEXT(s);
                    next = mp_parse_num_decimal("nan", 3, false, false, NULL);
CANDIDATE · ISSUE

ujson dumps exponential format but can't load it

closedby janpomopened 2019-05-13updated 2019-05-14
>>> import ujson
>>> d = {'foo': 1e10}
>>> ujson.dumps(d)
'{"foo": 1e+10}'
>>> ujson.loads(ujson.dumps(d))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid syntax for number

JSON standard does allow exponential format and the dumps seems to the right thing. The loads probably needs fixing.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied