This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: json.dump + indent creates trailing extra spaces
Type: resource usage Stage: resolved
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: josh.r, ned.deily, voxspox
Priority: normal Keywords:

Created on 2016-12-15 23:11 by voxspox, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg283354 - (view) Author: Knut (voxspox) Date: 2016-12-15 23:11
module json
python 2.7.12

json.dump(..., indent=4, sort_keys=True) gives me:

{
    "size": {
        "total": 19106,X
        "code": 18614,X
        "data": 492
    },X
    "next_item": 10
}


The "X" mark extra trailing space characters which are needless.
msg283355 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2016-12-16 00:43
This is documented behavior for Python 2 ( https://docs.python.org/2/library/json.html#basic-usage ):

>Note: Since the default item separator is ', ', the output might include trailing whitespace when indent is specified. You can use separators=(',', ': ') to avoid this.

and also:

If specified, separators should be an (item_separator, key_separator) tuple. By default, (', ', ': ') are used. To get the most compact JSON representation, you should specify (',', ':') to eliminate whitespace.

Python 3 gives a different guarantee (dynamically chosen default separators based on the value of indent), and it follows that contract.

Given it's clearly documented, easily worked around, and largely harmless, I see no strong argument for backporting the Python 3 behavior to the legacy codebase.
msg283356 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2016-12-16 01:01
I agree with Josh's assessment. Sorry!
msg283396 - (view) Author: Knut (voxspox) Date: 2016-12-16 10:17
sorry, I missed that point in the docs.
History
Date User Action Args
2022-04-11 14:58:40adminsetgithub: 73170
2016-12-16 10:17:12voxspoxsetmessages: + msg283396
2016-12-16 01:01:03ned.deilysetstatus: open -> closed

nosy: + ned.deily
messages: + msg283356

resolution: wont fix
stage: resolved
2016-12-16 00:43:13josh.rsetnosy: + josh.r
messages: + msg283355
2016-12-15 23:11:18voxspoxcreate