Skip to content
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

json.dumps with indent = 0 not adding newlines #54228

Closed
dpjanes mannequin opened this issue Oct 3, 2010 · 10 comments
Closed

json.dumps with indent = 0 not adding newlines #54228

dpjanes mannequin opened this issue Oct 3, 2010 · 10 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@dpjanes
Copy link
Mannequin

dpjanes mannequin commented Oct 3, 2010

BPO 10019
Nosy @etrepum, @amauryfa, @merwok, @bitdancer, @florentx, @sandrotosi
Files
  • json_indent0.patch
  • issue10019-py3.2.patch
  • issue10019-py3.1.patch
  • issue10019-py2.7.patch
  • 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 2011-04-13.01:24:16.598>
    created_at = <Date 2010-10-03.16:22:35.301>
    labels = ['type-bug', 'library']
    title = 'json.dumps with indent = 0 not adding newlines'
    updated_at = <Date 2011-04-13.01:24:16.596>
    user = 'https://bugs.python.org/dpjanes'

    bugs.python.org fields:

    activity = <Date 2011-04-13.01:24:16.596>
    actor = 'r.david.murray'
    assignee = 'none'
    closed = True
    closed_date = <Date 2011-04-13.01:24:16.598>
    closer = 'r.david.murray'
    components = ['Library (Lib)']
    creation = <Date 2010-10-03.16:22:35.301>
    creator = 'dpjanes'
    dependencies = []
    files = ['19124', '21630', '21631', '21632']
    hgrepos = []
    issue_num = 10019
    keywords = ['patch']
    message_count = 10.0
    messages = ['117917', '117929', '118166', '118167', '118168', '118749', '133594', '133595', '133628', '133630']
    nosy_count = 8.0
    nosy_names = ['bob.ippolito', 'amaury.forgeotdarc', 'eric.araujo', 'r.david.murray', 'flox', 'sandro.tosi', 'dpjanes', 'python-dev']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue10019'
    versions = ['Python 3.1', 'Python 2.7', 'Python 3.2', 'Python 3.3']

    @dpjanes
    Copy link
    Mannequin Author

    dpjanes mannequin commented Oct 3, 2010

    In Python 2.6.4, json.dumps(...,indent=0) produced newlines as documented here: http://docs.python.org/library/json.html#json.dump
    In Python 2.7, it no longer adds newlines.

    $ python
    Python 2.6.4 (r264:75706, Jan 13 2010, 19:41:08) 
    [GCC 4.1.2 20080704 (Red Hat 4.1.2-46)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import json
    >>> json.dumps({3:1,4:2},indent=0)
    '{\n"3": 1, \n"4": 2\n}'
    >>> print json.dumps({3:1,4:2},indent=0)
    {
    "3": 1, 
    "4": 2
    }
    
    
    
    $ python
    Python 2.7 (r27:82500, Oct  3 2010, 06:00:45) 
    [GCC 4.1.2 20080704 (Red Hat 4.1.2-46)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import json
    >>> json.dumps({3:1,4:2})
    '{"3": 1, "4": 2}'
    >>> print json.dumps({3:1,4:2},indent=0)
    {"3": 1, "4": 2}

    @dpjanes dpjanes mannequin added the stdlib Python modules in the Lib dir label Oct 3, 2010
    @amauryfa
    Copy link
    Member

    amauryfa commented Oct 3, 2010

    The C encoder should not be used when indent=0. Here is a patch+test for 2.7.
    Note that json.dump (into a file object) already correctly emit newlines.

    @etrepum
    Copy link
    Mannequin

    etrepum mannequin commented Oct 8, 2010

    The test in the patch assumes a specific iteration order for the dict "h", changing the dict to have only one key would fix this problem with the test.

    @etrepum
    Copy link
    Mannequin

    etrepum mannequin commented Oct 8, 2010

    The test also repeats an equivalent dict to "h" in the check function.

    @etrepum
    Copy link
    Mannequin

    etrepum mannequin commented Oct 8, 2010

    I just applied a version of this patch with corrections to the tests here:
    http://code.google.com/p/simplejson/source/detail?r=234

    @florentx
    Copy link
    Mannequin

    florentx mannequin commented Oct 15, 2010

    +1 to merge simplejson 2.1+ before 3.2 beta 1 (mid-november)

    @florentx florentx mannequin added the type-bug An unexpected behavior, bug, or error label Oct 15, 2010
    @sandrotosi
    Copy link
    Contributor

    Hi,
    I updated the patch, making one for 2.7, 3.1 and 3.2 (this last one applies cleanly on default too).

    As of merging simplejson, it's more a matter of porting it to Python 3. I'll drop an email to Bob soon, let's see how it goes.

    @sandrotosi
    Copy link
    Contributor

    Oh, just to say I took the version as of http://code.google.com/p/simplejson/source/detail?r=234

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Apr 13, 2011

    New changeset 8264f68e8251 by R David Murray in branch '2.7':
    bpo-10019: Fix regression relative to 2.6: add newlines if indent=0
    http://hg.python.org/cpython/rev/8264f68e8251

    New changeset 4a1048257995 by R David Murray in branch '3.1':
    bpo-10019: Fix regression relative to 2.6: add newlines if indent=0
    http://hg.python.org/cpython/rev/4a1048257995

    New changeset fe8bbaff5a27 by R David Murray in branch '3.2':
    Merge bpo-10019: Fix regression relative to 2.6: add newlines if indent=0
    http://hg.python.org/cpython/rev/fe8bbaff5a27

    New changeset 2d0d0850335e by R David Murray in branch 'default':
    Merge bpo-10019: Fix regression relative to 2.6: add newlines if indent=0
    http://hg.python.org/cpython/rev/2d0d0850335e

    @bitdancer
    Copy link
    Member

    Thanks, Sandro.

    @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 type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants