Skip to content

bpo-27413: json.tool: Add --no-ensure-ascii option. #17472

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

Merged
merged 1 commit into from
Dec 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Doc/library/json.rst
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,12 @@ Command line options

.. versionadded:: 3.5

.. cmdoption:: --no-ensure-ascii

Disable escaping of non-ascii characters, see :func:`json.dumps` for more information.

.. versionadded:: 3.9

.. cmdoption:: --json-lines

Parse every input line as separate JSON object.
Expand Down
3 changes: 3 additions & 0 deletions Lib/json/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def main():
default=sys.stdout)
parser.add_argument('--sort-keys', action='store_true', default=False,
help='sort the output of dictionaries alphabetically by key')
parser.add_argument('--no-ensure-ascii', dest='ensure_ascii', action='store_false',
help='disable escaping of non-ASCII characters')
parser.add_argument('--json-lines', action='store_true', default=False,
help='parse input using the jsonlines format')
group = parser.add_mutually_exclusive_group()
Expand All @@ -49,6 +51,7 @@ def main():
dump_args = {
'sort_keys': options.sort_keys,
'indent': options.indent,
'ensure_ascii': options.ensure_ascii,
}
if options.compact:
dump_args['indent'] = None
Expand Down
22 changes: 22 additions & 0 deletions Lib/test/test_json/test_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,25 @@ def test_compact(self):
json_stdout, err = proc.communicate(json_stdin)
self.assertEqual(expect.splitlines(), json_stdout.splitlines())
self.assertEqual(err, b'')

def test_no_ensure_ascii_flag(self):
infile = self._create_infile('{"key":"💩"}')
outfile = support.TESTFN + '.out'
self.addCleanup(os.remove, outfile)
assert_python_ok('-m', 'json.tool', '--no-ensure-ascii', infile, outfile)
with open(outfile, "rb") as f:
lines = f.read().splitlines()
# asserting utf-8 encoded output file
expected = [b'{', b' "key": "\xf0\x9f\x92\xa9"', b"}"]
self.assertEqual(lines, expected)

def test_ensure_ascii_default(self):
infile = self._create_infile('{"key":"💩"}')
outfile = support.TESTFN + '.out'
self.addCleanup(os.remove, outfile)
assert_python_ok('-m', 'json.tool', infile, outfile)
with open(outfile, "rb") as f:
lines = f.read().splitlines()
# asserting an ascii encoded output file
expected = [b'{', rb' "key": "\ud83d\udca9"', b"}"]
self.assertEqual(lines, expected)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added ability to pass through ``ensure_ascii`` options to json.dumps in the
``json.tool`` command-line interface.