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()
dump_args = {
'sort_keys': options.sort_keys,
'indent': options.indent,
+ 'ensure_ascii': options.ensure_ascii,
}
if options.compact:
dump_args['indent'] = None
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)