| # Gaudi Framework, version v21r8 |

Home |  | Generated: 17 Mar 2010

# tool.py

Go to the documentation of this file.

    00001 r"""Command-line tool to validate and pretty-print JSON
    00002
    00003 Usage::
    00004
    00005     $ echo '{"json":"obj"}' | python -m simplejson.tool
    00006     {
    00007         "json": "obj"
    00008     }
    00009     $ echo '{ 1.2:3.4}' | python -m simplejson.tool
    00010     Expecting property name: line 1 column 2 (char 2)
    00011
    00012 """
    00013 import sys
    00014 import simplejson
    00015
    00016 def main():
    00017     if len(sys.argv) == 1:
    00018         infile = sys.stdin
    00019         outfile = sys.stdout
    00020     elif len(sys.argv) == 2:
    00021         infile = open(sys.argv[1], 'rb')
    00022         outfile = sys.stdout
    00023     elif len(sys.argv) == 3:
    00024         infile = open(sys.argv[1], 'rb')
    00025         outfile = open(sys.argv[2], 'wb')
    00026     else:
    00027         raise SystemExit(sys.argv[0] + " [infile [outfile]]")
    00028     try:
    00029         obj = simplejson.load(infile)
    00030     except ValueError, e:
    00031         raise SystemExit(e)
    00032     simplejson.dump(obj, outfile, sort_keys=True, indent=4)
    00033     outfile.write('\n')
    00034
    00035
    00036 if __name__ == '__main__':
    00037     main()