Skip to content

Commit efefe25

Browse files
wimglennmethane
authored andcommitted
bpo-27413: json.tool: Add --no-ensure-ascii option. (GH-17472)
1 parent d863ade commit efefe25

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

Doc/library/json.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,12 @@ Command line options
732732

733733
.. versionadded:: 3.5
734734

735+
.. cmdoption:: --no-ensure-ascii
736+
737+
Disable escaping of non-ascii characters, see :func:`json.dumps` for more information.
738+
739+
.. versionadded:: 3.9
740+
735741
.. cmdoption:: --json-lines
736742

737743
Parse every input line as separate JSON object.

Lib/json/tool.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ def main():
3030
default=sys.stdout)
3131
parser.add_argument('--sort-keys', action='store_true', default=False,
3232
help='sort the output of dictionaries alphabetically by key')
33+
parser.add_argument('--no-ensure-ascii', dest='ensure_ascii', action='store_false',
34+
help='disable escaping of non-ASCII characters')
3335
parser.add_argument('--json-lines', action='store_true', default=False,
3436
help='parse input using the jsonlines format')
3537
group = parser.add_mutually_exclusive_group()
@@ -49,6 +51,7 @@ def main():
4951
dump_args = {
5052
'sort_keys': options.sort_keys,
5153
'indent': options.indent,
54+
'ensure_ascii': options.ensure_ascii,
5255
}
5356
if options.compact:
5457
dump_args['indent'] = None

Lib/test/test_json/test_tool.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,25 @@ def test_compact(self):
190190
json_stdout, err = proc.communicate(json_stdin)
191191
self.assertEqual(expect.splitlines(), json_stdout.splitlines())
192192
self.assertEqual(err, b'')
193+
194+
def test_no_ensure_ascii_flag(self):
195+
infile = self._create_infile('{"key":"💩"}')
196+
outfile = support.TESTFN + '.out'
197+
self.addCleanup(os.remove, outfile)
198+
assert_python_ok('-m', 'json.tool', '--no-ensure-ascii', infile, outfile)
199+
with open(outfile, "rb") as f:
200+
lines = f.read().splitlines()
201+
# asserting utf-8 encoded output file
202+
expected = [b'{', b' "key": "\xf0\x9f\x92\xa9"', b"}"]
203+
self.assertEqual(lines, expected)
204+
205+
def test_ensure_ascii_default(self):
206+
infile = self._create_infile('{"key":"💩"}')
207+
outfile = support.TESTFN + '.out'
208+
self.addCleanup(os.remove, outfile)
209+
assert_python_ok('-m', 'json.tool', infile, outfile)
210+
with open(outfile, "rb") as f:
211+
lines = f.read().splitlines()
212+
# asserting an ascii encoded output file
213+
expected = [b'{', rb' "key": "\ud83d\udca9"', b"}"]
214+
self.assertEqual(lines, expected)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Added ability to pass through ``ensure_ascii`` options to json.dumps in the
2+
``json.tool`` command-line interface.

0 commit comments

Comments
 (0)