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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/python-lib/simplejson/tool.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,44 @@
     1.4 +r"""
     1.5 +Using simplejson from the shell to validate and
     1.6 +pretty-print::
     1.7 +    
     1.8 +    $ echo '{"json":"obj"}' | python -msimplejson
     1.9 +    {
    1.10 +        "json": "obj"
    1.11 +    }
    1.12 +    $ echo '{ 1.2:3.4}' | python -msimplejson
    1.13 +    Expecting property name: line 1 column 2 (char 2)
    1.14 +
    1.15 +Note that the JSON produced by this module's default settings
    1.16 +is a subset of YAML, so it may be used as a serializer for that as well.
    1.17 +"""
    1.18 +import simplejson
    1.19 +
    1.20 +#
    1.21 +# Pretty printer:
    1.22 +#     curl http://mochikit.com/examples/ajax_tables/domains.json | python -msimplejson.tool
    1.23 +#
    1.24 +
    1.25 +def main():
    1.26 +    import sys
    1.27 +    if len(sys.argv) == 1:
    1.28 +        infile = sys.stdin
    1.29 +        outfile = sys.stdout
    1.30 +    elif len(sys.argv) == 2:
    1.31 +        infile = open(sys.argv[1], 'rb')
    1.32 +        outfile = sys.stdout
    1.33 +    elif len(sys.argv) == 3:
    1.34 +        infile = open(sys.argv[1], 'rb')
    1.35 +        outfile = open(sys.argv[2], 'wb')
    1.36 +    else:
    1.37 +        raise SystemExit("%s [infile [outfile]]" % (sys.argv[0],))
    1.38 +    try:
    1.39 +        obj = simplejson.load(infile)
    1.40 +    except ValueError, e:
    1.41 +        raise SystemExit(e)
    1.42 +    simplejson.dump(obj, outfile, sort_keys=True, indent=4)
    1.43 +    outfile.write('\n')
    1.44 +
    1.45 +
    1.46 +if __name__ == '__main__':
    1.47 +    main()

mercurial