app/simplejson/tool.py
author Pawel Solyga <Pawel.Solyga@gmail.com>
Sun, 25 Jan 2009 11:31:15 +0000
changeset 975 295d67509412
permissions -rw-r--r--
Add simplejson library to app folder. Patch by: Pawel Solyga Reviewed by: to-be-reviewed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
975
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     1
r"""Using simplejson from the shell to validate and
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     2
pretty-print::
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     3
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     4
    $ echo '{"json":"obj"}' | python -msimplejson.tool
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     5
    {
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     6
        "json": "obj"
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     7
    }
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     8
    $ echo '{ 1.2:3.4}' | python -msimplejson.tool
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     9
    Expecting property name: line 1 column 2 (char 2)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    10
"""
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    11
import simplejson
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    12
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    13
def main():
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    14
    import sys
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    15
    if len(sys.argv) == 1:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    16
        infile = sys.stdin
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    17
        outfile = sys.stdout
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    18
    elif len(sys.argv) == 2:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    19
        infile = open(sys.argv[1], 'rb')
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    20
        outfile = sys.stdout
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    21
    elif len(sys.argv) == 3:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    22
        infile = open(sys.argv[1], 'rb')
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    23
        outfile = open(sys.argv[2], 'wb')
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    24
    else:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    25
        raise SystemExit("%s [infile [outfile]]" % (sys.argv[0],))
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    26
    try:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    27
        obj = simplejson.load(infile)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    28
    except ValueError, e:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    29
        raise SystemExit(e)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    30
    simplejson.dump(obj, outfile, sort_keys=True, indent=4)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    31
    outfile.write('\n')
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    32
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    33
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    34
if __name__ == '__main__':
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    35
    main()