]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-27413: json.tool: Add --no-ensure-ascii option. (GH-17472)
authorwim glenn <hey@wimglenn.com>
Fri, 6 Dec 2019 06:44:01 +0000 (00:44 -0600)
committerInada Naoki <songofacandy@gmail.com>
Fri, 6 Dec 2019 06:44:01 +0000 (15:44 +0900)
Doc/library/json.rst
Lib/json/tool.py
Lib/test/test_json/test_tool.py
Misc/NEWS.d/next/Library/2019-12-05-02-02-58.bpo-27413.212Th2.rst [new file with mode: 0644]

index 23e39e95f783ef2cf49694b30b688e681f2e8123..573ec1cb77dd6e267563447eccb7f477046b3a17 100644 (file)
@@ -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.
index 2a404a44417960076915e9bca613c7782426404d..5542ce48c38023df6f9186fd617861931b8156ff 100644 (file)
@@ -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()
@@ -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
index 953a5696e7c22583f603a7c27f7a4927b110d94e..54800ae840c64c6940546a34314775f1f08a0dd1 100644 (file)
@@ -190,3 +190,25 @@ class TestTool(unittest.TestCase):
             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)
diff --git a/Misc/NEWS.d/next/Library/2019-12-05-02-02-58.bpo-27413.212Th2.rst b/Misc/NEWS.d/next/Library/2019-12-05-02-02-58.bpo-27413.212Th2.rst
new file mode 100644 (file)
index 0000000..0116b8c
--- /dev/null
@@ -0,0 +1,2 @@
+Added ability to pass through ``ensure_ascii`` options to json.dumps in the
+``json.tool`` command-line interface.