Aestate
tool.py
Go to the documentation of this file.
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 
18 def main():
19  if len(sys.argv) == 1:
20  infile = sys.stdin
21  outfile = sys.stdout
22  elif len(sys.argv) == 2:
23  infile = open(sys.argv[1], 'r')
24  outfile = sys.stdout
25  elif len(sys.argv) == 3:
26  infile = open(sys.argv[1], 'r')
27  outfile = open(sys.argv[2], 'w')
28  else:
29  raise SystemExit(sys.argv[0] + " [infile [outfile]]")
30  with infile:
31  try:
32  obj = json.load(infile,
33  object_pairs_hook=json.OrderedDict,
34  use_decimal=True)
35  except ValueError:
36  raise SystemExit(sys.exc_info()[1])
37  with outfile:
38  json.dump(obj, outfile, sort_keys=True, indent=' ', use_decimal=True)
39  outfile.write('\n')
40 
41 
42 if __name__ == '__main__':
43  main()
aestate.ajson.sim.tool.main
def main()
Definition: tool.py:18