Skip to content

json library is decoding/encoding when it should not #57421

Closed

Description

@thinred
mannequin
BPO 13212
Nosy @rhettinger, @etrepum, @pitrou, @vstinner, @ezio-melotti, @merwok, @florentx, @akheron

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 = 'https://github.com/etrepum'
closed_at = <Date 2014-11-27.19:34:05.235>
created_at = <Date 2011-10-18.18:00:31.817>
labels = ['type-bug', 'library']
title = 'json library is decoding/encoding when it should not'
updated_at = <Date 2014-11-27.19:34:05.234>
user = 'https://bugs.python.org/thinred'

bugs.python.org fields:

activity = <Date 2014-11-27.19:34:05.234>
actor = 'serhiy.storchaka'
assignee = 'bob.ippolito'
closed = True
closed_date = <Date 2014-11-27.19:34:05.235>
closer = 'serhiy.storchaka'
components = ['Library (Lib)']
creation = <Date 2011-10-18.18:00:31.817>
creator = 'thinred'
dependencies = []
files = []
hgrepos = []
issue_num = 13212
keywords = []
message_count = 21.0
messages = ['145847', '145853', '145888', '145895', '145916', '145917', '145995', '146142', '146144', '146147', '146152', '168941', '168960', '170158', '170194', '213991', '218642', '219142', '219143', '219401', '231772']
nosy_count = 11.0
nosy_names = ['rhettinger', 'bob.ippolito', 'pitrou', 'vstinner', 'ezio.melotti', 'eric.araujo', 'cvrebert', 'flox', 'petri.lehtinen', 'thinred', 'hac.man']
pr_nums = []
priority = 'high'
resolution = 'wont fix'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue13212'
versions = ['Python 2.7', 'Python 3.2', 'Python 3.3']

Activity

  1. thinred commented on Oct 18, 2011

    thinredmannequin
    MannequinAuthor

    Hi there!
    According to http://www.ietf.org/rfc/rfc4627.txt . The JSON object is either a list or a dictionary with other elements in it. However Python's implementation took liberty to parse also basic types, like integers, strings, floats, etc. For example:

    >>> import json
    >>> json.loads('42')
    42
    >>> json.dumps(3.141592)
    '3.141592'

    It wouldn't be a problem if other implementations would adhere to this behavior, but this is not true. Take Ruby for example:

    irb(main):007:0> require 'rubygems';
    irb(main):008:0* require 'json';
    irb(main):009:0* JSON.parse '42'
    JSON::ParserError: 743: unexpected token at '42'
    from /var/lib/gems/1.8/gems/json-1.6.1/lib/json/common.rb:148:in parse' from /var/lib/gems/1.8/gems/json-1.6.1/lib/json/common.rb:148:in parse'
    from (irb):9
    from �:0
    irb(main):010:0> JSON.generate(3.141592)
    JSON::GeneratorError: only generation of JSON objects or arrays allowed
    from /var/lib/gems/1.8/gems/json-1.6.1/lib/json/common.rb:216:in generate' from /var/lib/gems/1.8/gems/json-1.6.1/lib/json/common.rb:216:in generate'
    from (irb):10
    from �:0

    Also http://jsonlint.com/ considers it an error.

    I know this is not very important, but I've ran into this today when trying to parse JSON in Ruby which was produced in Python.

    I've made a patch for that, but I still need to fix tests and there are many of them. I first would like to know if it is going to be fixed, then I will complete the patch.

    Thanks for Python, anyway!

    Tomek

  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Oct 18, 2011
  3. rhettinger commented on Oct 18, 2011

    @rhettinger
    Contributor

    I don't see any way of changing this without breaking lots of code that currently works fine. It may be possible to add a strict-mode (off by default) or to add warnings.

  4. ezio-melotti commented on Oct 19, 2011

    @ezio-melotti
    Member

    I wonder if there are concrete use cases for loading/dumping basic types.
    I see two options here:

    1. we decide that load/dump should accept and produce only complete json object and go through a deprecation process, raising warnings first and errors then;
    2. we decide that the correct behavior (loading/dumping basic types) is fine and that should be up to the user to check if the document has an array/object at the top level and then just document it (if it's not documented already).
  5. thinred commented on Oct 19, 2011

    thinredmannequin
    MannequinAuthor

    It wouldn't be a problem if 'json' library would be a thing specific to Python. But JSON is used to gule together different technologies and programming languages. I'm afraid that the default behavior should be 1).

  6. rhettinger commented on Oct 19, 2011

    @rhettinger
    Contributor

    I'm thinking that we should offer a strict-option (off by default) and leave it at that. No need to break existing code. AFAICT, the OP is the only one to have had problems with this in-practice (the JSON module or its ancestor simplejson has been around for a long time).

    Am marking this as a high priority but want to leave it open for a good while to get more comments before taking any action.

  7. self-assigned this
    on Oct 19, 2011
  8. pitrou commented on Oct 19, 2011

    @pitrou
    Member

    Agreed that a strict parameter would be a good idea to implement this.
    Since it's technically a new feature (and not that important), I think only 3.3 should receive it.

  9. ezio-melotti commented on Oct 20, 2011

    @ezio-melotti
    Member

    I'm not sure adding a strict option is a good idea. HTMLParser got a strict argument and it's causing more problems than it solves. If another problem comes up later we would either have to change the behavior for strict and break code that by then is using strict=True or add yet another really_strict argument.

  10. merwok commented on Oct 21, 2011

    @merwok
    Member

    HTMLParser got a strict argument and it's causing more problems than it solves.
    Funny, I used the strict mode to solve a parsing bug in a personal project.

  11. ezio-melotti commented on Oct 21, 2011

    @ezio-melotti
    Member

    > HTMLParser got a strict argument and it's causing more problems than it solves.
    Funny, I used the strict mode to solve a parsing bug in a personal project.
    See http://bugs.python.org/issue7311#msg133075 and following messages.r

  12. rhettinger commented on Oct 21, 2011

    @rhettinger
    Contributor

    Assigning back to me (the current maintainer of the module).

  13. self-assigned this
    on Oct 21, 2011
  14. 1 remaining item

  15. thinred commented on Aug 23, 2012

    thinredmannequin
    MannequinAuthor

    Take a look at http://bugs.python.org/issue14674.
    A proposed documentation patch specifically discusses this issue.

  16. akheron commented on Aug 23, 2012

    @akheron
    Member

    The strict flag sounds good to me, too. I don't think it should be called "strict" though, as that would imply that we comply with RFC 4627 strictly (which is not true without passing allow_nan=False for dump() or passing a parse_constant function for load()). How about "encode_any" and "decode_any" or "any_value"?

    Deprecation doesn't sound good to me. If the feature is already there, it probably has its users and use cases.

  17. hacman commented on Sep 10, 2012

    hacmanmannequin
    Mannequin

    I think it's worth pointing out that both Firefox and Chrome support the non-standard JSON that Python supports (serializing and deserializing basic types). I'm guessing that communicating with web browsers is the vast majority of JSON IPC. That is to say, supporting the de-facto standard implemented by web browsers may be better than adhering to the exact specifications of the RFC.

    Maybe someone else wants to check what IE, Safari, Opera, and the various phone browsers allow as that might influece the discussion one way or another.

  18. merwok commented on Sep 10, 2012

    @merwok
    Member

    I don't think it should be called "strict" though, as that would imply that we comply with RFC 4627 strictly
    (which is not true without passing allow_nan=False for dump() or passing a parse_constant function for load())

    What if the strict mode implied both of these things?

    FWIW, I’m +1 for a doc section about how to achieve strict mode with special arguments and callbacks (if the recent doc patch does not already have that), and +0 on adding it to the code itself.

  19. cvrebert commented on Mar 18, 2014

    cvrebertmannequin
    Mannequin

    FWIW, I’m +1 for a doc section about how to achieve strict mode with special arguments and callbacks (if the recent doc patch does not already have that)

    The docs added by that patch do indeed cover this:
    http://docs.python.org/2/library/json.html#standard-compliance

  20. cvrebert commented on May 16, 2014

    cvrebertmannequin
    Mannequin

    Note that, per the new JSON RFC 7159 (https://tools.ietf.org/html/rfc7159 ), top-level non-collection values are now de-jure permissible in JSON:

    Appendix A. Changes from RFC 4627
    o Changed the definition of "JSON text" so that it can be any JSON
    value, removing the constraint that it be an object or array.

  21. etrepum commented on May 26, 2014

    etrepummannequin
    Mannequin

    As Chris Rebert mentioned, the JSON standards have adopted this (unsurprising) behavior. Ruby hasn't, and I doubt Crockford has, but I think they're in the minority at this point. JavaScript's own JSON implementation works the same way json/simplejson does.

    JSON.parse(JSON.stringify('yay'))
    "yay"

    At best, I think it's probably worth a mention in the documentation, but not worth changing any code over.

  22. etrepum commented on May 26, 2014

    etrepummannequin
    Mannequin

    In other words, I would consider this to be fixed by the documentation change made elsewhere.

  23. cvrebert commented on May 30, 2014

    cvrebertmannequin
    Mannequin

    Okay, so can this issue be closed in light of the existing docs and bpo-21514 then?

  24. cvrebert commented on Nov 27, 2014

    cvrebertmannequin
    Mannequin

    Ping!
    Seems like this should be closed since the new RFC explicitly legalizes the feature in question and since the docs explicitly warn about the interoperability of the feature.

  25. transferred this issue fromon Apr 10, 2022
  26. deleted a comment from jparsem on Apr 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions