App Engine Python SDK version 1.7.4 (2)
[gae.git] / python / lib / django_1_4 / django / utils / simplejson / tool.py
blob74401c279a7920245ab9706c954df24a004e4cd0
1 r"""Using simplejson from the shell to validate and
2 pretty-print::
4 $ echo '{"json":"obj"}' | python -msimplejson.tool
6 "json": "obj"
8 $ echo '{ 1.2:3.4}' | python -msimplejson.tool
9 Expecting property name: line 1 column 2 (char 2)
10 """
11 from django.utils import simplejson
13 def main():
14 import sys
15 if len(sys.argv) == 1:
16 infile = sys.stdin
17 outfile = sys.stdout
18 elif len(sys.argv) == 2:
19 infile = open(sys.argv[1], 'rb')
20 outfile = sys.stdout
21 elif len(sys.argv) == 3:
22 infile = open(sys.argv[1], 'rb')
23 outfile = open(sys.argv[2], 'wb')
24 else:
25 raise SystemExit("%s [infile [outfile]]" % (sys.argv[0],))
26 try:
27 obj = simplejson.load(infile)
28 except ValueError, e:
29 raise SystemExit(e)
30 simplejson.dump(obj, outfile, sort_keys=True, indent=4)
31 outfile.write('\n')
34 if __name__ == '__main__':
35 main()