Skip to content

Python JSON module doesn't actually produce JSON #70293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ConnorWolf mannequin opened this issue Jan 13, 2016 · 3 comments
Closed

Python JSON module doesn't actually produce JSON #70293

ConnorWolf mannequin opened this issue Jan 13, 2016 · 3 comments
Labels
stdlib Python modules in the Lib dir

Comments

@ConnorWolf
Copy link
Mannequin

ConnorWolf mannequin commented Jan 13, 2016

BPO 26105
Nosy @ezio-melotti

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2016-01-13.23:19:58.735>
created_at = <Date 2016-01-13.23:15:13.646>
labels = ['invalid', 'library']
title = "Python JSON module doesn't actually produce JSON"
updated_at = <Date 2016-01-13.23:40:34.233>
user = 'https://bugs.python.org/ConnorWolf'

bugs.python.org fields:

activity = <Date 2016-01-13.23:40:34.233>
actor = 'Connor.Wolf'
assignee = 'none'
closed = True
closed_date = <Date 2016-01-13.23:19:58.735>
closer = 'ezio.melotti'
components = ['Library (Lib)']
creation = <Date 2016-01-13.23:15:13.646>
creator = 'Connor.Wolf'
dependencies = []
files = []
hgrepos = []
issue_num = 26105
keywords = []
message_count = 3.0
messages = ['258179', '258181', '258183']
nosy_count = 2.0
nosy_names = ['ezio.melotti', 'Connor.Wolf']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue26105'
versions = ['Python 3.5', 'Python 3.6']

@ConnorWolf
Copy link
Mannequin Author

ConnorWolf mannequin commented 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 be null.

JSON *does not allow NaN, infinity, or -infinity.

json.dump[s] has the parameter allow_nan, but it's False by default, so basically it's not actually JSON by default.

The default for emitting JSON should actually emit JSON. allow_nan must be True by default.

@ConnorWolf ConnorWolf mannequin added the stdlib Python modules in the Lib dir label Jan 13, 2016
@ezio-melotti
Copy link
Member

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
Copy link
Mannequin Author

ConnorWolf mannequin commented 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.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir
Projects
None yet
Development

No branches or pull requests

1 participant