summaryrefslogtreecommitdiffstats
path: root/module/lib/simplejson/encoder.py
diff options
context:
space:
mode:
Diffstat (limited to 'module/lib/simplejson/encoder.py')
-rw-r--r--module/lib/simplejson/encoder.py49
1 files changed, 40 insertions, 9 deletions
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):