22
33import argparse
44import io
5+ import json
56import sys
67from collections import OrderedDict
78
8- import simplejson
9- import six
9+ from six import text_type
1010
1111
1212def _get_pretty_format (contents , indent , ensure_ascii = True , sort_keys = True , top_keys = []):
@@ -17,40 +17,28 @@ def pairs_first(pairs):
1717 if sort_keys :
1818 after = sorted (after , key = lambda x : x [0 ])
1919 return OrderedDict (before + after )
20- return six .text_type (simplejson .dumps (
21- simplejson .loads (
22- contents ,
23- object_pairs_hook = pairs_first ,
24- ),
20+ json_pretty = json .dumps (
21+ json .loads (contents , object_pairs_hook = pairs_first ),
2522 indent = indent ,
2623 ensure_ascii = ensure_ascii ,
27- )) + "\n " # dumps does not end with a newline
24+ separators = (',' , ': ' ), # Workaround for https://bugs.python.org/issue16333
25+ )
26+ # Ensure unicode (Py2) and add the newline that dumps does not end with.
27+ return text_type (json_pretty ) + '\n '
2828
2929
3030def _autofix (filename , new_contents ):
31- print (" Fixing file {}" .format (filename ))
31+ print (' Fixing file {}' .format (filename ))
3232 with io .open (filename , 'w' , encoding = 'UTF-8' ) as f :
3333 f .write (new_contents )
3434
3535
36- def parse_indent (s ):
37- # type: (str) -> str
36+ def parse_num_to_int (s ):
37+ """Convert string numbers to int, leaving strings as is."""
3838 try :
39- int_indentation_spec = int (s )
39+ return int (s )
4040 except ValueError :
41- if not s .strip ():
42- return s
43- else :
44- raise ValueError (
45- 'Non-whitespace JSON indentation delimiter supplied. ' ,
46- )
47- else :
48- if int_indentation_spec >= 0 :
49- return int_indentation_spec * ' '
50- else :
51- raise ValueError (
52- 'Negative integer supplied to construct JSON indentation delimiter. ' ,
53- )
41+ return s
5442
5543
5644def parse_topkeys (s ):
@@ -68,9 +56,12 @@ def pretty_format_json(argv=None):
6856 )
6957 parser .add_argument (
7058 '--indent' ,
71- type = parse_indent ,
72- default = ' ' ,
73- help = 'String used as delimiter for one indentation level' ,
59+ type = parse_num_to_int ,
60+ default = '2' ,
61+ help = (
62+ 'The number of indent spaces or a string to be used as delimiter'
63+ ' for indentation level e.g. 4 or "\t " (Default: 2)'
64+ ),
7465 )
7566 parser .add_argument (
7667 '--no-ensure-ascii' ,
@@ -110,16 +101,15 @@ def pretty_format_json(argv=None):
110101 )
111102
112103 if contents != pretty_contents :
113- print (" File {} is not pretty-formatted" .format (json_file ))
104+ print (' File {} is not pretty-formatted' .format (json_file ))
114105
115106 if args .autofix :
116107 _autofix (json_file , pretty_contents )
117108
118109 status = 1
119-
120- except simplejson .JSONDecodeError :
110+ except ValueError :
121111 print (
122- " Input File {} is not a valid JSON, consider using check-json"
112+ ' Input File {} is not a valid JSON, consider using check-json'
123113 .format (json_file ),
124114 )
125115 return 1
0 commit comments