rename files
[wikipedia-api-cdsw] / simplejson / tool.py
1 r"""Command-line tool to validate and pretty-print JSON
2
3 Usage::
4
5     $ echo '{"json":"obj"}' | python -m simplejson.tool
6     {
7         "json": "obj"
8     }
9     $ echo '{ 1.2:3.4}' | python -m simplejson.tool
10     Expecting property name: line 1 column 2 (char 2)
11
12 """
13 from __future__ import with_statement
14 import sys
15 import simplejson as json
16
17 def main():
18     if len(sys.argv) == 1:
19         infile = sys.stdin
20         outfile = sys.stdout
21     elif len(sys.argv) == 2:
22         infile = open(sys.argv[1], 'r')
23         outfile = sys.stdout
24     elif len(sys.argv) == 3:
25         infile = open(sys.argv[1], 'r')
26         outfile = open(sys.argv[2], 'w')
27     else:
28         raise SystemExit(sys.argv[0] + " [infile [outfile]]")
29     with infile:
30         try:
31             obj = json.load(infile,
32                             object_pairs_hook=json.OrderedDict,
33                             use_decimal=True)
34         except ValueError:
35             raise SystemExit(sys.exc_info()[1])
36     with outfile:
37         json.dump(obj, outfile, sort_keys=True, indent='    ', use_decimal=True)
38         outfile.write('\n')
39
40
41 if __name__ == '__main__':
42     main()

Benjamin Mako Hill || Want to submit a patch?