micropython / micropython/micropython
ujson dumps NaN but doesn't load it
Open
Nobody has claimed this yet.
enhancement
- Dominant language
- C
- Stars
- 22.1k
- Forks
- 9k
- Avg merge
- 6d 4h
- Merged PRs (30d)
- 16
Description
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);
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in modujson.c around line 115 and inspect mod_ujson_load's handling of numeric values. Reproduce the shown ujson.dumps/loads sequence, then verify that loading the dumped NaN no longer raises a JSON syntax error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c, python
- Domain
- embedded-iot
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100