diff options
Diffstat (limited to 'module')
-rw-r--r-- | module/common/json_layer.py | 9 | ||||
-rw-r--r-- | module/lib/simplejson/.___init__.py | bin | 187 -> 0 bytes | |||
-rw-r--r-- | module/lib/simplejson/._decoder.py | bin | 187 -> 0 bytes | |||
-rw-r--r-- | module/lib/simplejson/._scanner.py | bin | 184 -> 0 bytes | |||
-rw-r--r-- | module/lib/simplejson/__init__.py | 54 | ||||
-rw-r--r-- | module/lib/simplejson/encoder.py | 49 |
6 files changed, 87 insertions, 25 deletions
diff --git a/module/common/json_layer.py b/module/common/json_layer.py index a075d2527..7b1c80bb5 100644 --- a/module/common/json_layer.py +++ b/module/common/json_layer.py @@ -8,6 +8,9 @@ try: # since python 2.6 from json import loads as json_loads from json import dumps as json_dumps except ImportError: - import module.lib.simplejson as json - from module.lib.simplejson import loads as json_loads - from module.lib.simplejson import dumps as json_dumps
\ No newline at end of file + try: + import module.lib.simplejson as json + from module.lib.simplejson import loads as json_loads + from module.lib.simplejson import dumps as json_dumps + except: + print "Could not import simplejson" diff --git a/module/lib/simplejson/.___init__.py b/module/lib/simplejson/.___init__.py Binary files differdeleted file mode 100644 index cee8587de..000000000 --- a/module/lib/simplejson/.___init__.py +++ /dev/null diff --git a/module/lib/simplejson/._decoder.py b/module/lib/simplejson/._decoder.py Binary files differdeleted file mode 100644 index 9c13fd41c..000000000 --- a/module/lib/simplejson/._decoder.py +++ /dev/null diff --git a/module/lib/simplejson/._scanner.py b/module/lib/simplejson/._scanner.py Binary files differdeleted file mode 100644 index d352b5550..000000000 --- a/module/lib/simplejson/._scanner.py +++ /dev/null diff --git a/module/lib/simplejson/__init__.py b/module/lib/simplejson/__init__.py index 210b957a9..ef5c0db48 100644 --- a/module/lib/simplejson/__init__.py +++ b/module/lib/simplejson/__init__.py @@ -97,7 +97,7 @@ Using simplejson.tool from the shell to validate and pretty-print:: $ echo '{ 1.2:3.4}' | python -m simplejson.tool Expecting property name: line 1 column 2 (char 2) """ -__version__ = '2.1.6' +__version__ = '2.2.1' __all__ = [ 'dump', 'dumps', 'load', 'loads', 'JSONDecoder', 'JSONDecodeError', 'JSONEncoder', @@ -135,12 +135,16 @@ _default_encoder = JSONEncoder( separators=None, encoding='utf-8', default=None, - use_decimal=False, + use_decimal=True, + namedtuple_as_object=True, + tuple_as_array=True, ) def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, - encoding='utf-8', default=None, use_decimal=False, **kw): + encoding='utf-8', default=None, use_decimal=True, + namedtuple_as_object=True, tuple_as_array=True, + **kw): """Serialize ``obj`` as a JSON formatted stream to ``fp`` (a ``.write()``-supporting file-like object). @@ -179,9 +183,16 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, ``default(obj)`` is a function that should return a serializable version of obj or raise TypeError. The default simply raises TypeError. - If *use_decimal* is true (default: ``False``) then decimal.Decimal + If *use_decimal* is true (default: ``True``) then decimal.Decimal will be natively serialized to JSON with full precision. + If *namedtuple_as_object* is true (default: ``True``), + :class:`tuple` subclasses with ``_asdict()`` methods will be encoded + as JSON objects. + + If *tuple_as_array* is true (default: ``True``), + :class:`tuple` (and subclasses) will be encoded as JSON arrays. + To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the ``.default()`` method to serialize additional types), specify it with the ``cls`` kwarg. @@ -191,8 +202,8 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, if (not skipkeys and ensure_ascii and check_circular and allow_nan and cls is None and indent is None and separators is None and - encoding == 'utf-8' and default is None and not use_decimal - and not kw): + encoding == 'utf-8' and default is None and use_decimal + and namedtuple_as_object and tuple_as_array and not kw): iterable = _default_encoder.iterencode(obj) else: if cls is None: @@ -200,7 +211,10 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii, check_circular=check_circular, allow_nan=allow_nan, indent=indent, separators=separators, encoding=encoding, - default=default, use_decimal=use_decimal, **kw).iterencode(obj) + default=default, use_decimal=use_decimal, + namedtuple_as_object=namedtuple_as_object, + tuple_as_array=tuple_as_array, + **kw).iterencode(obj) # could accelerate with writelines in some versions of Python, at # a debuggability cost for chunk in iterable: @@ -209,7 +223,10 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, - encoding='utf-8', default=None, use_decimal=False, **kw): + encoding='utf-8', default=None, use_decimal=True, + namedtuple_as_object=True, + tuple_as_array=True, + **kw): """Serialize ``obj`` to a JSON formatted ``str``. If ``skipkeys`` is false then ``dict`` keys that are not basic types @@ -245,9 +262,16 @@ def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, ``default(obj)`` is a function that should return a serializable version of obj or raise TypeError. The default simply raises TypeError. - If *use_decimal* is true (default: ``False``) then decimal.Decimal + If *use_decimal* is true (default: ``True``) then decimal.Decimal will be natively serialized to JSON with full precision. + If *namedtuple_as_object* is true (default: ``True``), + :class:`tuple` subclasses with ``_asdict()`` methods will be encoded + as JSON objects. + + If *tuple_as_array* is true (default: ``True``), + :class:`tuple` (and subclasses) will be encoded as JSON arrays. + To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the ``.default()`` method to serialize additional types), specify it with the ``cls`` kwarg. @@ -257,8 +281,8 @@ def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, if (not skipkeys and ensure_ascii and check_circular and allow_nan and cls is None and indent is None and separators is None and - encoding == 'utf-8' and default is None and not use_decimal - and not kw): + encoding == 'utf-8' and default is None and use_decimal + and namedtuple_as_object and tuple_as_array and not kw): return _default_encoder.encode(obj) if cls is None: cls = JSONEncoder @@ -266,7 +290,10 @@ def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, skipkeys=skipkeys, ensure_ascii=ensure_ascii, check_circular=check_circular, allow_nan=allow_nan, indent=indent, separators=separators, encoding=encoding, default=default, - use_decimal=use_decimal, **kw).encode(obj) + use_decimal=use_decimal, + namedtuple_as_object=namedtuple_as_object, + tuple_as_array=tuple_as_array, + **kw).encode(obj) _default_decoder = JSONDecoder(encoding=None, object_hook=None, @@ -275,7 +302,8 @@ _default_decoder = JSONDecoder(encoding=None, object_hook=None, def load(fp, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, - use_decimal=False, **kw): + use_decimal=False, namedtuple_as_object=True, tuple_as_array=True, + **kw): """Deserialize ``fp`` (a ``.read()``-supporting file-like object containing a JSON document) to a Python object. diff --git a/module/lib/simplejson/encoder.py b/module/lib/simplejson/encoder.py index f43f6f430..5ec7440f1 100644 --- a/module/lib/simplejson/encoder.py +++ b/module/lib/simplejson/encoder.py @@ -13,7 +13,7 @@ c_encode_basestring_ascii, c_make_encoder = _import_speedups() from simplejson.decoder import PosInf -ESCAPE = re.compile(r'[\x00-\x1f\\"\b\f\n\r\t]') +ESCAPE = re.compile(ur'[\x00-\x1f\\"\b\f\n\r\t\u2028\u2029]') ESCAPE_ASCII = re.compile(r'([\\"]|[^\ -~])') HAS_UTF8 = re.compile(r'[\x80-\xff]') ESCAPE_DCT = { @@ -24,6 +24,8 @@ ESCAPE_DCT = { '\n': '\\n', '\r': '\\r', '\t': '\\t', + u'\u2028': '\\u2028', + u'\u2029': '\\u2029', } for i in range(0x20): #ESCAPE_DCT.setdefault(chr(i), '\\u{0:04x}'.format(i)) @@ -78,7 +80,7 @@ class JSONEncoder(object): +-------------------+---------------+ | Python | JSON | +===================+===============+ - | dict | object | + | dict, namedtuple | object | +-------------------+---------------+ | list, tuple | array | +-------------------+---------------+ @@ -104,7 +106,8 @@ class JSONEncoder(object): def __init__(self, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, encoding='utf-8', default=None, - use_decimal=False): + use_decimal=True, namedtuple_as_object=True, + tuple_as_array=True): """Constructor for JSONEncoder, with sensible defaults. If skipkeys is false, then it is a TypeError to attempt @@ -152,6 +155,11 @@ class JSONEncoder(object): be supported directly by the encoder. For the inverse, decode JSON with ``parse_float=decimal.Decimal``. + If namedtuple_as_object is true (the default), tuple subclasses with + ``_asdict()`` methods will be encoded as JSON objects. + + If tuple_as_array is true (the default), tuple (and subclasses) will + be encoded as JSON arrays. """ self.skipkeys = skipkeys @@ -160,6 +168,8 @@ class JSONEncoder(object): self.allow_nan = allow_nan self.sort_keys = sort_keys self.use_decimal = use_decimal + self.namedtuple_as_object = namedtuple_as_object + self.tuple_as_array = tuple_as_array if isinstance(indent, (int, long)): indent = ' ' * indent self.indent = indent @@ -274,12 +284,14 @@ class JSONEncoder(object): _iterencode = c_make_encoder( markers, self.default, _encoder, self.indent, self.key_separator, self.item_separator, self.sort_keys, - self.skipkeys, self.allow_nan, key_memo, self.use_decimal) + self.skipkeys, self.allow_nan, key_memo, self.use_decimal, + self.namedtuple_as_object, self.tuple_as_array) else: _iterencode = _make_iterencode( markers, self.default, _encoder, self.indent, floatstr, self.key_separator, self.item_separator, self.sort_keys, - self.skipkeys, _one_shot, self.use_decimal) + self.skipkeys, _one_shot, self.use_decimal, + self.namedtuple_as_object, self.tuple_as_array) try: return _iterencode(o, 0) finally: @@ -315,7 +327,7 @@ class JSONEncoderForHTML(JSONEncoder): def _make_iterencode(markers, _default, _encoder, _indent, _floatstr, _key_separator, _item_separator, _sort_keys, _skipkeys, _one_shot, - _use_decimal, + _use_decimal, _namedtuple_as_object, _tuple_as_array, ## HACK: hand-optimized bytecode; turn globals into locals False=False, True=True, @@ -373,7 +385,13 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr, yield buf + str(value) else: yield buf - if isinstance(value, (list, tuple)): + if isinstance(value, list): + chunks = _iterencode_list(value, _current_indent_level) + elif (_namedtuple_as_object and isinstance(value, tuple) and + hasattr(value, '_asdict')): + chunks = _iterencode_dict(value._asdict(), + _current_indent_level) + elif _tuple_as_array and isinstance(value, tuple): chunks = _iterencode_list(value, _current_indent_level) elif isinstance(value, dict): chunks = _iterencode_dict(value, _current_indent_level) @@ -452,7 +470,13 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr, elif _use_decimal and isinstance(value, Decimal): yield str(value) else: - if isinstance(value, (list, tuple)): + if isinstance(value, list): + chunks = _iterencode_list(value, _current_indent_level) + elif (_namedtuple_as_object and isinstance(value, tuple) and + hasattr(value, '_asdict')): + chunks = _iterencode_dict(value._asdict(), + _current_indent_level) + elif _tuple_as_array and isinstance(value, tuple): chunks = _iterencode_list(value, _current_indent_level) elif isinstance(value, dict): chunks = _iterencode_dict(value, _current_indent_level) @@ -480,7 +504,14 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr, yield str(o) elif isinstance(o, float): yield _floatstr(o) - elif isinstance(o, (list, tuple)): + elif isinstance(o, list): + for chunk in _iterencode_list(o, _current_indent_level): + yield chunk + elif (_namedtuple_as_object and isinstance(o, tuple) and + hasattr(o, '_asdict')): + for chunk in _iterencode_dict(o._asdict(), _current_indent_level): + yield chunk + elif (_tuple_as_array and isinstance(o, tuple)): for chunk in _iterencode_list(o, _current_indent_level): yield chunk elif isinstance(o, dict): |