diff options
author | Armin Ronacher <armin.ronacher@active-4.com> | 2008-06-26 11:11:20 +0000 |
---|---|---|
committer | Armin Ronacher <armin.ronacher@active-4.com> | 2008-06-26 11:11:20 +0000 |
commit | 11498d0037437789cab7b8ced69ba0101d97c786 (patch) | |
tree | bb6d7e7104daffe8cbb63ae708be580741d1bdc4 /sphinx/util/json.py | |
parent | 95f7883e94fd42379d9eea8337fcf3ec93d28d5a (diff) | |
download | sphinx-git-11498d0037437789cab7b8ced69ba0101d97c786.tar.gz |
Implemented JSONHTMLBuilder and improved JSON handling (it now prefers json from the 2.6 stdlib or simplejson).
Diffstat (limited to 'sphinx/util/json.py')
-rw-r--r-- | sphinx/util/json.py | 105 |
1 files changed, 25 insertions, 80 deletions
diff --git a/sphinx/util/json.py b/sphinx/util/json.py index ad4a58ac3..1686fe6a5 100644 --- a/sphinx/util/json.py +++ b/sphinx/util/json.py @@ -3,87 +3,32 @@ sphinx.util.json ~~~~~~~~~~~~~~~~ - Minimal JSON module that generates small dumps. + This module imports JSON functions from various locations. - This is not fully JSON compliant but enough for the searchindex. - And the generated files are smaller than the simplejson ones. - - Uses the basestring encode function from simplejson. - - :copyright: 2007-2008 by Armin Ronacher, Bob Ippolito. + :copyright: Copyright 2008 by Armin Ronacher. :license: BSD. """ -import re - -# escape \, ", control characters and everything outside ASCII -ESCAPE_ASCII = re.compile(r'([\\"]|[^\ -~])') -ESCAPE_DICT = { - '\\': '\\\\', - '"': '\\"', - '\b': '\\b', - '\f': '\\f', - '\n': '\\n', - '\r': '\\r', - '\t': '\\t', -} - - -def encode_basestring_ascii(s): - def replace(match): - s = match.group(0) - try: - return ESCAPE_DICT[s] - except KeyError: - n = ord(s) - if n < 0x10000: - return '\\u%04x' % (n,) - else: - # surrogate pair - n -= 0x10000 - s1 = 0xd800 | ((n >> 10) & 0x3ff) - s2 = 0xdc00 | (n & 0x3ff) - return '\\u%04x\\u%04x' % (s1, s2) - return '"' + str(ESCAPE_ASCII.sub(replace, s)) + '"' - - -def dump_json(obj, key=False): - if key: - if not isinstance(obj, basestring): - obj = str(obj) - return encode_basestring_ascii(obj) - if obj is None: - return 'null' - elif obj is True or obj is False: - return obj and 'true' or 'false' - elif isinstance(obj, (int, long, float)): - return str(obj) - elif isinstance(obj, dict): - return '{%s}' % ','.join('%s:%s' % ( - dump_json(key, True), - dump_json(value) - ) for key, value in obj.iteritems()) - elif isinstance(obj, (tuple, list, set)): - return '[%s]' % ','.join(dump_json(x) for x in obj) - elif isinstance(obj, basestring): - return encode_basestring_ascii(obj) - raise TypeError(type(obj)) - - -STRING = re.compile(r'("(\\\\|\\"|[^"])*")') - -def load_json(s): - d = {'null': None, 'true': True, 'false': False} - s = STRING.sub(r'u\1', s) - return eval(s, d) - - -# serializer interface -dumps = dump_json -loads = load_json - -def dump(obj, f): - f.write(dumps(obj)) - -def load(f): - return loads(f.read()) +# if no simplejson is available this module can not load json files. +can_load = True + +# unset __name__ for a moment so that the import goes straight into +# the stdlib for python 2.4. +_old_name = __name__ +del __name__ + +try: + from simplejson import dumps, dump, loads, load +except ImportError: + try: + from json import dumps, dump, loads, load + except ImportError: + from sphinx.util._json import dumps, dump + def _dummy(x): + raise NotImplementedError('simplejson unavailable, can\'t load') + load = loads = _dummy + can_load = False + del _dummy + +__name__ = _old_name +del _old_name |