# HG changeset patch # User Wei-Cheng Pan # Date 1467198054 -28800 # Wed Jun 29 19:00:54 2016 +0800 # Node ID 46d6d8759907fa308ddfbc55f5d7dd37fae27e65 # Parent 8d7bde14d7a48f747b1ea2f77385352c507da6b9 json: Add an option to bypass non-ASCII characters. diff --git a/Lib/json/tool.py b/Lib/json/tool.py --- a/Lib/json/tool.py +++ b/Lib/json/tool.py @@ -22,29 +22,32 @@ def main(): 'to validate and pretty-print JSON objects.') parser = argparse.ArgumentParser(prog=prog, description=description) parser.add_argument('infile', nargs='?', type=argparse.FileType(), 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('--sort-keys', action='store_true', default=False, help='sort the output of dictionaries alphabetically by key') + parser.add_argument('--no-ensure-ascii', action='store_true', default=False, + help='output non-ASCII characters as-is') options = parser.parse_args() infile = options.infile or sys.stdin outfile = options.outfile or sys.stdout sort_keys = options.sort_keys + ensure_ascii = not options.no_ensure_ascii with infile: try: if sort_keys: obj = json.load(infile) else: obj = json.load(infile, object_pairs_hook=collections.OrderedDict) except ValueError as e: raise SystemExit(e) with outfile: - json.dump(obj, outfile, sort_keys=sort_keys, indent=4) + json.dump(obj, outfile, ensure_ascii=ensure_ascii, sort_keys=sort_keys, indent=4) outfile.write('\n') if __name__ == '__main__': main()