Skip to content

Allow parsing NaN, Infinity #73

@jobh

Description

@jobh

The standard library JSON parser accepts NaN and Infinity float constants - which are not part of strict JSON but quite common to accept regardless.

>>> import json, json_stream
>>> json.loads(b"[NaN, Infinity, -Infinity]")
[nan, inf, -inf]
>>> list(json_stream.load([b"[NaN, Infinity, -Infinity]"]))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/jobh/mambaforge/envs/gass/lib/python3.12/site-packages/json_stream/base.py", line 50, in _iter_items
    item = self._load_item()
           ^^^^^^^^^^^^^^^^^
  File "/home/jobh/mambaforge/envs/gass/lib/python3.12/site-packages/json_stream/base.py", line 195, in _load_item
    item = super()._load_item()
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/jobh/mambaforge/envs/gass/lib/python3.12/site-packages/json_stream/base.py", line 144, in _load_item
    token_type, v = next(self._stream)
                    ^^^^^^^^^^^^^^^^^^
ValueError: Invalid JSON character: 'N' at index 1

Would it be possible to support these javascript constants in json-stream? My workaround is a sed -e 's/NaN/null/g' preprocessing step - which is acceptable for my specific use-case, but fragile.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions