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
Python JSON module doesn't actually produce JSON #70293
The problem here is that JSON is *everywhere*, and I only ran into this particular issue after a whole bunch of digging as to why my "JSON" messages were disappearing in some javascript.
Basically, with the default the way it is, you have interoperability bombs in every project that uses it to interface with other languages. In my case, I'm using Flask-SocketIO ( https://github.com/miguelgrinberg/Flask-SocketIO ), which uses JSON as it's transport, and it works fine until you have a NaN or infinity in your data, at which point the socket.io in the browser starts *silently* eating messages.
Basically, if I call json.dumps, the principle of least astonishment dictated that you actually get, you know, JSON.
If you have a module called something like pyson, and it's partially JSON compatible, that makes sense. For the JSON module to fail at the very thing it's named after is kind of ludicrous.
Reacted by Dennis, Aurélien Campéas and Andrew Kay
Activity
ConnorWolf commentedon Jan 13, 2016
The Python library JSON library doesn't emit JSON by default.
Basically,
json.dumps(float('nan'))produces something that kind of looks like json, but isn't (specifically,'NaN'). Valid JSON must benull.JSON *does not allow
NaN,infinity, or-infinity.json.dump[s]has the parameterallow_nan, but it'sFalseby default, so basically it's not actually JSON by default.The default for emitting JSON should actually emit JSON.
allow_nanmust beTrueby default.ezio-melotti commentedon Jan 13, 2016
This is documented at https://docs.python.org/3/library/json.html#standard-compliance-and-interoperability and https://docs.python.org/3/library/json.html#infinite-and-nan-number-values
The default could be changed if we go through a deprecation process, but I'm not sure it's worth it.
ConnorWolf commentedon Jan 13, 2016
The problem here is that JSON is *everywhere*, and I only ran into this particular issue after a whole bunch of digging as to why my "JSON" messages were disappearing in some javascript.
Basically, with the default the way it is, you have interoperability bombs in every project that uses it to interface with other languages. In my case, I'm using Flask-SocketIO ( https://github.com/miguelgrinberg/Flask-SocketIO ), which uses JSON as it's transport, and it works fine until you have a NaN or infinity in your data, at which point the socket.io in the browser starts *silently* eating messages.
Basically, if I call json.dumps, the principle of least astonishment dictated that you actually get, you know, JSON.
If you have a module called something like
pyson, and it's partially JSON compatible, that makes sense. For the JSON module to fail at the very thing it's named after is kind of ludicrous.