Skip to content

Python JSON module doesn't actually produce JSON #70293

Closed

Description

@ConnorWolf
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']

Activity

  1. ConnorWolf commented on Jan 13, 2016

    ConnorWolfmannequin
    MannequinAuthor

    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.

  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Jan 13, 2016
  3. ezio-melotti commented on Jan 13, 2016

    @ezio-melotti
    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.

  4. ConnorWolf commented on Jan 13, 2016

    ConnorWolfmannequin
    MannequinAuthor

    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.

  5. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directory

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions