Skip to content

Infinity/-Infinity/NaN values produce invalid JSON #1979

Closed

Description

@mattrobenolt
>>> json.dumps({'x': float('inf')})
'{"x": Infinity}’
> JSON.parse('{"x": Infinity}')
SyntaxError: Unexpected token I
>>> json.loads('{"x": Infinity}')
{u'x': inf}

So if a value has inf in it, we end up returning invalid JSON to the browser, which causes all sorts of fun.

dumps provides an option allow_nan=False which doesn't allow these values to get encoded, but in turn, it raises a ValueError and rejects encoding the entire blob, which isn't really desirable either since it results in the same issue of not being able to get the data in a browser.

Activity

  1. self-assigned this
    on Sep 18, 2015
  2. mattrobenolt commented on Sep 18, 2015

    @mattrobenolt
    ContributorAuthor

    Ok, this is super shitty.

    First of all, this behavior is hardcoded into json.JSONEncoder as seen.
    https://github.com/python/cpython/blob/2.7/Lib/json/encoder.py#L236-L256

    We can attempt to get around this by overriding our own entire iterencode and replace the floatstr function to do what we want.

    But if you look closely, we end up hitting the C encoder path: https://github.com/python/cpython/blob/2.7/Lib/json/encoder.py#L261 Which doesn't pass along that function.

    Instead, we move along into C and get to: https://github.com/python/cpython/blob/2.7/Modules/_json.c#L1940-L1962

    So afaict, there are a couple options here:

    • We skip the C encoder entirely and just use the pure python one, which will handle this correctly. (I don't recommend)
    • We attempt to encode twice, first with C encoder, then catch the raised ValueError, and retry again with pure python version.
    • Use a regular expression to remove Inifinity, -Infinity, and NaN from the string JSON output.
    • We supply our own forked C _json module. (I'd really prefer we not do this)

    I'm going to vote for option 2, attempting to encode twice, especially since this case is rare, so it should be fine.

    Thoughts @mitsuhiko @dcramer ?

  3. mattrobenolt commented on Sep 18, 2015

    @mattrobenolt
    ContributorAuthor

    Another alternative here is to use simplejson with ignore_nan as it turns out, which will spit back null for those values.

  4. added a commit that references this issue on Sep 18, 2015
    f500c96
  5. mattrobenolt commented on Sep 18, 2015

    @mattrobenolt
    ContributorAuthor
  6. locked and limited conversation to collaborators on Dec 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions