classification
Title: json.dumps with indent = 0 not adding newlines
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.1, Python 3.2, Python 3.3, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, bob.ippolito, dpjanes, eric.araujo, flox, python-dev, r.david.murray, sandro.tosi
Priority: normal Keywords: patch

Created on 2010-10-03 16:22 by dpjanes, last changed 2011-04-13 01:24 by r.david.murray. This issue is now closed.

Files
File name Uploaded Description Edit
json_indent0.patch amaury.forgeotdarc, 2010-10-03 19:50 review
issue10019-py3.2.patch sandro.tosi, 2011-04-12 18:13 review
issue10019-py3.1.patch sandro.tosi, 2011-04-12 18:13
issue10019-py2.7.patch sandro.tosi, 2011-04-12 18:13
Messages (10)
msg117917 - (view) Author: David Janes (dpjanes) Date: 2010-10-03 16:22
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}
msg117929 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2010-10-03 19:50
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.
msg118166 - (view) Author: Bob Ippolito (bob.ippolito) * (Python committer) Date: 2010-10-08 07:35
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.
msg118167 - (view) Author: Bob Ippolito (bob.ippolito) * (Python committer) Date: 2010-10-08 07:39
The test also repeats an equivalent dict to "h" in the check function.
msg118168 - (view) Author: Bob Ippolito (bob.ippolito) * (Python committer) Date: 2010-10-08 07:44
I just applied a version of this patch with corrections to the tests here:
http://code.google.com/p/simplejson/source/detail?r=234
msg118749 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-10-15 08:42
+1 to merge simplejson 2.1+ before 3.2 beta 1 (mid-november)
msg133594 - (view) Author: Sandro Tosi (sandro.tosi) * (Python committer) Date: 2011-04-12 18:13
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.
msg133595 - (view) Author: Sandro Tosi (sandro.tosi) * (Python committer) Date: 2011-04-12 18:15
Oh, just to say I took the version as of http://code.google.com/p/simplejson/source/detail?r=234
msg133628 - (view) Author: Roundup Robot (python-dev) Date: 2011-04-13 01:22
New changeset 8264f68e8251 by R David Murray in branch '2.7':
#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':
#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 #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 #10019: Fix regression relative to 2.6: add newlines if indent=0
http://hg.python.org/cpython/rev/2d0d0850335e
msg133630 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2011-04-13 01:24
Thanks, Sandro.
History
Date User Action Args
2011-04-13 01:24:16r.david.murraysetstatus: open -> closed

assignee: bob.ippolito ->

nosy: + r.david.murray
messages: + msg133630
resolution: accepted -> fixed
stage: commit review -> resolved
2011-04-13 01:22:31python-devsetnosy: + python-dev
messages: + msg133628
2011-04-12 18:15:12sandro.tosisetmessages: + msg133595
2011-04-12 18:13:48sandro.tosisetfiles: + issue10019-py2.7.patch
2011-04-12 18:13:36sandro.tosisetfiles: + issue10019-py3.1.patch
2011-04-12 18:13:16sandro.tosisetfiles: + issue10019-py3.2.patch
versions: + Python 3.3
nosy: + sandro.tosi

messages: + msg133594

stage: patch review -> commit review
2010-10-24 18:25:13eric.araujosetnosy: + eric.araujo
2010-10-15 08:42:38floxsettype: behavior
versions: + Python 3.1, Python 3.2
nosy: + flox

messages: + msg118749
resolution: accepted
stage: patch review
2010-10-08 07:44:57bob.ippolitosetmessages: + msg118168
2010-10-08 07:39:04bob.ippolitosetmessages: + msg118167
2010-10-08 07:35:36bob.ippolitosetmessages: + msg118166
2010-10-03 19:50:46amaury.forgeotdarcsetfiles: + json_indent0.patch

nosy: + amaury.forgeotdarc
messages: + msg117929

keywords: + patch
2010-10-03 19:21:01pitrousetassignee: bob.ippolito

nosy: + bob.ippolito
2010-10-03 16:22:35dpjanescreate