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

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 r"""
     2 Using simplejson from the shell to validate and
     3 pretty-print::
     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)
    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
    17 #
    18 # Pretty printer:
    19 #     curl http://mochikit.com/examples/ajax_tables/domains.json | python -msimplejson.tool
    20 #
    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')
    43 if __name__ == '__main__':
    44     main()

mercurial