Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

bpo-29636: Add --indent / --no-indent arguments to json.tool #345

Open
wants to merge 4 commits into
from

Conversation

Projects
None yet
4 participants
Contributor

dhimmel commented Feb 27, 2017

From #201 which is being split into two pull requests.

Tagging @serhiy-storchaka, @berkerpeksag, @methane who helped review #201.

Closes http://bugs.python.org/issue29636.

Contributor

dhimmel commented Feb 27, 2017

Note to rebase on #346 which should be merged first.

+ group.add_argument('--indent', default=4, type=int,
+ help='Indent level (number of spaces) for '
+ 'pretty-printing. Defaults to 4.')
+ group.add_argument('--no-indent', action='store_const', dest='indent',
@haypo

haypo Mar 2, 2017

Member

I would prefer a --compact option which use indent=None but also separators without spaces: http://bugs.python.org/issue29540

It's not the same than what you propose.

@dhimmel

dhimmel Mar 3, 2017 edited

Contributor

@haypo I agree the most compact representation would be useful.

One option would be add a third argument (--compact) to the mutually exclusive group. This would provide access to three options:

  1. --indent: pretty (with newlines)
  2. --no-indent: with indent=None in json.dump
  3. --compact: compact, with separators=(',', ':') in json.dump

Alternatively, are you proposing to only support options 1 & 3?

I'm happy to implement this. Let's get a second opinion, so I don't go down the wrong track. Tagging @serhiy-storchaka, @berkerpeksag, @methane.

@haypo

haypo Mar 3, 2017

Member

Is --no-indent differen than --indent=0?

Would it be possible to use a default of 4 spaces when using --indent with no argument?

@serhiy-storchaka

serhiy-storchaka Mar 3, 2017

Member

Is --no-indent differen than --indent=0?

Yes, it is. --indent=0 adds new lines, --no-indent writes all in one line.

Would it be possible to use a default of 4 spaces when using --indent with no argument?

This is the default behavior.

@dhimmel

dhimmel Mar 3, 2017

Contributor

@haypo the following illustrates the three options

import json
obj = [1, 2]

print('--indent=4')
print(json.dumps(obj, indent=4))

print('--no-indent')
print(json.dumps(obj, indent=None))

print('--compact')
print(json.dumps(obj, separators=(',', ':')))

which produces the following output

--indent=4
[
    1,
    2
]
--no-indent
[1, 2]
--compact
[1,2]
@dhimmel

dhimmel Mar 3, 2017

Contributor

Spaces after comma and semicolon are not just for readability. They make JSON compatible with YAML 1.0.

@serhiy-storchaka great point. I think this is a strong argument for not replacing --no-indent with --compact. However, should we add --compact as a third option? I can see the utility for users who wan't the smallest file sizes.

Member

serhiy-storchaka commented Mar 3, 2017

Spaces after comma and semicolon are not just for readability. They make JSON compatible with YAML 1.0.

Member

haypo commented Mar 3, 2017

Member

serhiy-storchaka commented Mar 3, 2017

This is not the right place for the design discussion. Lets continue it on the bug tracker.

Contributor

dhimmel commented Mar 3, 2017

This is not the right place for the design discussion. Lets continue it on the bug tracker.

@serhiy-storchaka and @haypo I updated bpo-29636 with a summary of the design discussion thus far.

I propose continuing this pull request with only --indent and --no-indent. If we end up reaching consensus to add --compact, that can be added in the future. I think it would fit in nicely alongside --indent and --no-indent. In other words, it's not pressing to decide on --compact right now.

Contributor

dhimmel commented Mar 16, 2017

#346 has been merged and this PR rebased.

@serhiy-storchaka, @berkerpeksag, @methane and @haypo: open for review. No more outstanding changes on my end.

dhimmel added some commits Jul 15, 2017

@dhimmel dhimmel bpo-30971: Improve code readability of json.tool
Originall from #2720

Set default option for infile and outfile as per
#2720 (review)

Use --sort-keys help message based on the json.tool docs at
https://docs.python.org/3.6/library/json.html#basic-usage:

Remove commens as per https://bugs.python.org/msg298692.
Code was descriptive without the comments.
fadb45e
@dhimmel dhimmel Add --indent / --no-indent arguments to json.tool
From #201 which is being split into
two pull requests.

See http://bugs.python.org/issue29636
3c67514
@dhimmel dhimmel json.tool: --tab identation option 70cc07b
@dhimmel dhimmel json.tool: --compact identation option
a098b5d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment