You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Infinity/-Infinity/NaN values produce invalid JSON #1979
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
mattrobenolt commentedon Sep 18, 2015
Ok, this is super shitty.
First of all, this behavior is hardcoded into
json.JSONEncoderas 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
iterencodeand replace thefloatstrfunction 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:
ValueError, and retry again with pure python version.Inifinity,-Infinity, andNaNfrom the string JSON output.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 ?
mattrobenolt commentedon Sep 18, 2015
Another alternative here is to use
simplejsonwithignore_nanas it turns out, which will spit backnullfor those values.Switch to simplejson for encoding json
mattrobenolt commentedon Sep 18, 2015
Turns out, this needs to be handled within
rest_frameworktoo. https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/renderers.py#L53