-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-27413: add --no-ensure-ascii argument to json.tool #201
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
a47f396
fb5d984
4fb3bb3
29d01fe
a3e2b23
97152e9
d699070
0f38c18
66a173a
dd25cf3
3413f4f
52bafb0
1306518
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 |
---|---|---|
|
@@ -25,24 +25,37 @@ def main(): | |
help='a JSON file to be validated or pretty-printed') | ||
parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'), | ||
help='write the output of infile to outfile') | ||
parser.add_argument('--no-ensure-ascii', action='store_true', default=False, | ||
help='Do not set ensure_ascii to escape non-ASCII characters') | ||
group = parser.add_mutually_exclusive_group() | ||
group.add_argument('--indent', default=4, type=int, | ||
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. Needed tests for |
||
help='Indent level for pretty-printing.') | ||
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. Document default value. 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. Done in 66a173a. |
||
group.add_argument('--no-indent', action='store_true', default=False, | ||
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. Maybe use 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. Nice idea. Implemented in 0f38c18. As an aside, # error: argument --no-indent: not allowed with argument --indent
echo "[1,2,3]" | python Lib/json/tool.py --indent=5 --no-indent
# No error, compact mode
echo "[1,2,3]" | python Lib/json/tool.py --indent=4 --no-indent
# No error, indented
echo "[1,2,3]" | python Lib/json/tool.py --no-indent --indent=4 Something to keep in mind... assuming this will be fixed. |
||
help='Use compact mode.') | ||
parser.add_argument('--sort-keys', action='store_true', default=False, | ||
help='sort the output of dictionaries alphabetically by key') | ||
options = parser.parse_args() | ||
|
||
# Read input JSON | ||
infile = options.infile or sys.stdin | ||
outfile = options.outfile or sys.stdout | ||
sort_keys = options.sort_keys | ||
with infile: | ||
try: | ||
if sort_keys: | ||
if options.sort_keys: | ||
obj = json.load(infile) | ||
else: | ||
obj = json.load(infile, | ||
object_pairs_hook=collections.OrderedDict) | ||
except ValueError as e: | ||
raise SystemExit(e) | ||
|
||
# Export JSON | ||
outfile = options.outfile or sys.stdout | ||
with outfile: | ||
json.dump(obj, outfile, sort_keys=sort_keys, indent=4) | ||
json.dump(obj, outfile, | ||
indent=None if options.no_indent else options.indent, | ||
ensure_ascii=not options.no_ensure_ascii, | ||
sort_keys=options.sort_keys, | ||
) | ||
outfile.write('\n') | ||
|
||
|
||
|
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 option name should be
--ensure_ascii
, (and default should be True).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.
@methane are you suggesting:
The problem here is that it's counterintuitive for
--ensure_ascii
to result inensure_ascii=False
. In https://bugs.python.org/issue27413, the discussion settled on--no-ensure-ascii
, which I'm happy to switch to:Just let me know what's preferred.