addon-sdk/source/python-lib/simplejson/tool.py

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:ce885ac502db
1 r"""
2 Using simplejson from the shell to validate and
3 pretty-print::
4
5 $ echo '{"json":"obj"}' | python -msimplejson
6 {
7 "json": "obj"
8 }
9 $ echo '{ 1.2:3.4}' | python -msimplejson
10 Expecting property name: line 1 column 2 (char 2)
11
12 Note that the JSON produced by this module's default settings
13 is a subset of YAML, so it may be used as a serializer for that as well.
14 """
15 import simplejson
16
17 #
18 # Pretty printer:
19 # curl http://mochikit.com/examples/ajax_tables/domains.json | python -msimplejson.tool
20 #
21
22 def main():
23 import sys
24 if len(sys.argv) == 1:
25 infile = sys.stdin
26 outfile = sys.stdout
27 elif len(sys.argv) == 2:
28 infile = open(sys.argv[1], 'rb')
29 outfile = sys.stdout
30 elif len(sys.argv) == 3:
31 infile = open(sys.argv[1], 'rb')
32 outfile = open(sys.argv[2], 'wb')
33 else:
34 raise SystemExit("%s [infile [outfile]]" % (sys.argv[0],))
35 try:
36 obj = simplejson.load(infile)
37 except ValueError, e:
38 raise SystemExit(e)
39 simplejson.dump(obj, outfile, sort_keys=True, indent=4)
40 outfile.write('\n')
41
42
43 if __name__ == '__main__':
44 main()

mercurial