-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-29636: Add --indent / --no-indent arguments to json.tool #345
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
Changes from 7 commits
fadb45e
3c67514
70cc07b
a098b5d
ba16891
3d3477d
b8fee34
292fc1c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -21,23 +21,42 @@ def main(): | |||||
'to validate and pretty-print JSON objects.') | ||||||
parser = argparse.ArgumentParser(prog=prog, description=description) | ||||||
parser.add_argument('infile', nargs='?', type=argparse.FileType(), | ||||||
default=sys.stdin, | ||||||
help='a JSON file to be validated or pretty-printed') | ||||||
parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'), | ||||||
default=sys.stdout, | ||||||
help='write the output of infile to outfile') | ||||||
parser.add_argument('--sort-keys', action='store_true', default=False, | ||||||
help='sort the output of dictionaries alphabetically by key') | ||||||
parser.add_argument('--sort-keys', action='store_true', | ||||||
help='sort the output of dictionaries by key') | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why removed "alphabetically"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe my intention was to make the help message more closely mirror the Lines 157 to 158 in cfa797c
Happy to re-add "alphabetically" if that was helpful. |
||||||
group = parser.add_mutually_exclusive_group() | ||||||
group.add_argument('--indent', default=4, type=int, | ||||||
help='separate items with newlines and use this number ' | ||||||
'of spaces for indentation') | ||||||
group.add_argument('--tab', action='store_const', dest='indent', | ||||||
const='\t', help='separate items with newlines and use ' | ||||||
'tabs for indentation') | ||||||
group.add_argument('--no-indent', action='store_const', dest='indent', | ||||||
const=None, | ||||||
help='separate items with spaces rather than newlines') | ||||||
group.add_argument('--compact', action='store_true', | ||||||
help='suppress all whitespace separation (most compact)') | ||||||
options = parser.parse_args() | ||||||
|
||||||
infile = options.infile or sys.stdin | ||||||
outfile = options.outfile or sys.stdout | ||||||
sort_keys = options.sort_keys | ||||||
with infile: | ||||||
with options.infile as infile: | ||||||
try: | ||||||
obj = json.load(infile) | ||||||
except ValueError as e: | ||||||
raise SystemExit(e) | ||||||
with outfile: | ||||||
json.dump(obj, outfile, sort_keys=sort_keys, indent=4) | ||||||
|
||||||
kwargs = { | ||||||
'indent': options.indent, | ||||||
'sort_keys': options.sort_keys, | ||||||
} | ||||||
if options.compact: | ||||||
kwargs['indent'] = None | ||||||
kwargs['separators'] = ',', ':' | ||||||
with options.outfile as outfile: | ||||||
json.dump(obj, outfile, **kwargs) | ||||||
outfile.write('\n') | ||||||
|
||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Add whitespace options for formatting JSON with the ``json.tool`` CLI. The | ||
following mutually exclusive options are now supported: ``--indent`` for | ||
setting the indent level in spaces; ``--tab`` for indenting with tabs; | ||
``--no-indent`` for suppressing newlines; and ``--compact`` for suppressing | ||
all whitespace. The default behavior remains the same as ``--indent=4``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the benefit of setting default to
sys.stdin
instead of keeping itNone
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This way a suggestion from another contributor at #2720 (comment):
By adding
default=sys.stdin
, we remove the following line later on: