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

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     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