Skip to content

Commit a2cdb4e

Browse files
committed
updated simplejson
1 parent 57b1f15 commit a2cdb4e

6 files changed

Lines changed: 87 additions & 25 deletions

File tree

‎module/common/json_layer.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
from json import loads as json_loads
99
from json import dumps as json_dumps
1010
except ImportError:
11-
import module.lib.simplejson as json
12-
from module.lib.simplejson import loads as json_loads
13-
from module.lib.simplejson import dumps as json_dumps
11+
try:
12+
import module.lib.simplejson as json
13+
from module.lib.simplejson import loads as json_loads
14+
from module.lib.simplejson import dumps as json_dumps
15+
except:
16+
print "Could not import simplejson"
-187 Bytes
Binary file not shown.

‎module/lib/simplejson/._decoder.py‎

-187 Bytes
Binary file not shown.

‎module/lib/simplejson/._scanner.py‎

-184 Bytes
Binary file not shown.

‎module/lib/simplejson/__init__.py‎

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
$ echo '{ 1.2:3.4}' | python -m simplejson.tool
9898
Expecting property name: line 1 column 2 (char 2)
9999
"""
100-
__version__ = '2.1.6'
100+
__version__ = '2.2.1'
101101
__all__ = [
102102
'dump', 'dumps', 'load', 'loads',
103103
'JSONDecoder', 'JSONDecodeError', 'JSONEncoder',
@@ -135,12 +135,16 @@ def _import_c_make_encoder():
135135
separators=None,
136136
encoding='utf-8',
137137
default=None,
138-
use_decimal=False,
138+
use_decimal=True,
139+
namedtuple_as_object=True,
140+
tuple_as_array=True,
139141
)
140142

141143
def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
142144
allow_nan=True, cls=None, indent=None, separators=None,
143-
encoding='utf-8', default=None, use_decimal=False, **kw):
145+
encoding='utf-8', default=None, use_decimal=True,
146+
namedtuple_as_object=True, tuple_as_array=True,
147+
**kw):
144148
"""Serialize ``obj`` as a JSON formatted stream to ``fp`` (a
145149
``.write()``-supporting file-like object).
146150
@@ -179,9 +183,16 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
179183
``default(obj)`` is a function that should return a serializable version
180184
of obj or raise TypeError. The default simply raises TypeError.
181185
182-
If *use_decimal* is true (default: ``False``) then decimal.Decimal
186+
If *use_decimal* is true (default: ``True``) then decimal.Decimal
183187
will be natively serialized to JSON with full precision.
184188
189+
If *namedtuple_as_object* is true (default: ``True``),
190+
:class:`tuple` subclasses with ``_asdict()`` methods will be encoded
191+
as JSON objects.
192+
193+
If *tuple_as_array* is true (default: ``True``),
194+
:class:`tuple` (and subclasses) will be encoded as JSON arrays.
195+
185196
To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
186197
``.default()`` method to serialize additional types), specify it with
187198
the ``cls`` kwarg.
@@ -191,16 +202,19 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
191202
if (not skipkeys and ensure_ascii and
192203
check_circular and allow_nan and
193204
cls is None and indent is None and separators is None and
194-
encoding == 'utf-8' and default is None and not use_decimal
195-
and not kw):
205+
encoding == 'utf-8' and default is None and use_decimal
206+
and namedtuple_as_object and tuple_as_array and not kw):
196207
iterable = _default_encoder.iterencode(obj)
197208
else:
198209
if cls is None:
199210
cls = JSONEncoder
200211
iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii,
201212
check_circular=check_circular, allow_nan=allow_nan, indent=indent,
202213
separators=separators, encoding=encoding,
203-
default=default, use_decimal=use_decimal, **kw).iterencode(obj)
214+
default=default, use_decimal=use_decimal,
215+
namedtuple_as_object=namedtuple_as_object,
216+
tuple_as_array=tuple_as_array,
217+
**kw).iterencode(obj)
204218
# could accelerate with writelines in some versions of Python, at
205219
# a debuggability cost
206220
for chunk in iterable:
@@ -209,7 +223,10 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
209223

210224
def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
211225
allow_nan=True, cls=None, indent=None, separators=None,
212-
encoding='utf-8', default=None, use_decimal=False, **kw):
226+
encoding='utf-8', default=None, use_decimal=True,
227+
namedtuple_as_object=True,
228+
tuple_as_array=True,
229+
**kw):
213230
"""Serialize ``obj`` to a JSON formatted ``str``.
214231
215232
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,
245262
``default(obj)`` is a function that should return a serializable version
246263
of obj or raise TypeError. The default simply raises TypeError.
247264
248-
If *use_decimal* is true (default: ``False``) then decimal.Decimal
265+
If *use_decimal* is true (default: ``True``) then decimal.Decimal
249266
will be natively serialized to JSON with full precision.
250267
268+
If *namedtuple_as_object* is true (default: ``True``),
269+
:class:`tuple` subclasses with ``_asdict()`` methods will be encoded
270+
as JSON objects.
271+
272+
If *tuple_as_array* is true (default: ``True``),
273+
:class:`tuple` (and subclasses) will be encoded as JSON arrays.
274+
251275
To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
252276
``.default()`` method to serialize additional types), specify it with
253277
the ``cls`` kwarg.
@@ -257,16 +281,19 @@ def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
257281
if (not skipkeys and ensure_ascii and
258282
check_circular and allow_nan and
259283
cls is None and indent is None and separators is None and
260-
encoding == 'utf-8' and default is None and not use_decimal
261-
and not kw):
284+
encoding == 'utf-8' and default is None and use_decimal
285+
and namedtuple_as_object and tuple_as_array and not kw):
262286
return _default_encoder.encode(obj)
263287
if cls is None:
264288
cls = JSONEncoder
265289
return cls(
266290
skipkeys=skipkeys, ensure_ascii=ensure_ascii,
267291
check_circular=check_circular, allow_nan=allow_nan, indent=indent,
268292
separators=separators, encoding=encoding, default=default,
269-
use_decimal=use_decimal, **kw).encode(obj)
293+
use_decimal=use_decimal,
294+
namedtuple_as_object=namedtuple_as_object,
295+
tuple_as_array=tuple_as_array,
296+
**kw).encode(obj)
270297

271298

272299
_default_decoder = JSONDecoder(encoding=None, object_hook=None,
@@ -275,7 +302,8 @@ def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
275302

276303
def load(fp, encoding=None, cls=None, object_hook=None, parse_float=None,
277304
parse_int=None, parse_constant=None, object_pairs_hook=None,
278-
use_decimal=False, **kw):
305+
use_decimal=False, namedtuple_as_object=True, tuple_as_array=True,
306+
**kw):
279307
"""Deserialize ``fp`` (a ``.read()``-supporting file-like object containing
280308
a JSON document) to a Python object.
281309

‎module/lib/simplejson/encoder.py‎

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def _import_speedups():
1313

1414
from simplejson.decoder import PosInf
1515

16-
ESCAPE = re.compile(r'[\x00-\x1f\\"\b\f\n\r\t]')
16+
ESCAPE = re.compile(ur'[\x00-\x1f\\"\b\f\n\r\t\u2028\u2029]')
1717
ESCAPE_ASCII = re.compile(r'([\\"]|[^\ -~])')
1818
HAS_UTF8 = re.compile(r'[\x80-\xff]')
1919
ESCAPE_DCT = {
@@ -24,6 +24,8 @@ def _import_speedups():
2424
'\n': '\\n',
2525
'\r': '\\r',
2626
'\t': '\\t',
27+
u'\u2028': '\\u2028',
28+
u'\u2029': '\\u2029',
2729
}
2830
for i in range(0x20):
2931
#ESCAPE_DCT.setdefault(chr(i), '\\u{0:04x}'.format(i))
@@ -78,7 +80,7 @@ class JSONEncoder(object):
7880
+-------------------+---------------+
7981
| Python | JSON |
8082
+===================+===============+
81-
| dict | object |
83+
| dict, namedtuple | object |
8284
+-------------------+---------------+
8385
| list, tuple | array |
8486
+-------------------+---------------+
@@ -104,7 +106,8 @@ class JSONEncoder(object):
104106
def __init__(self, skipkeys=False, ensure_ascii=True,
105107
check_circular=True, allow_nan=True, sort_keys=False,
106108
indent=None, separators=None, encoding='utf-8', default=None,
107-
use_decimal=False):
109+
use_decimal=True, namedtuple_as_object=True,
110+
tuple_as_array=True):
108111
"""Constructor for JSONEncoder, with sensible defaults.
109112
110113
If skipkeys is false, then it is a TypeError to attempt
@@ -152,6 +155,11 @@ def __init__(self, skipkeys=False, ensure_ascii=True,
152155
be supported directly by the encoder. For the inverse, decode JSON
153156
with ``parse_float=decimal.Decimal``.
154157
158+
If namedtuple_as_object is true (the default), tuple subclasses with
159+
``_asdict()`` methods will be encoded as JSON objects.
160+
161+
If tuple_as_array is true (the default), tuple (and subclasses) will
162+
be encoded as JSON arrays.
155163
"""
156164

157165
self.skipkeys = skipkeys
@@ -160,6 +168,8 @@ def __init__(self, skipkeys=False, ensure_ascii=True,
160168
self.allow_nan = allow_nan
161169
self.sort_keys = sort_keys
162170
self.use_decimal = use_decimal
171+
self.namedtuple_as_object = namedtuple_as_object
172+
self.tuple_as_array = tuple_as_array
163173
if isinstance(indent, (int, long)):
164174
indent = ' ' * indent
165175
self.indent = indent
@@ -274,12 +284,14 @@ def floatstr(o, allow_nan=self.allow_nan,
274284
_iterencode = c_make_encoder(
275285
markers, self.default, _encoder, self.indent,
276286
self.key_separator, self.item_separator, self.sort_keys,
277-
self.skipkeys, self.allow_nan, key_memo, self.use_decimal)
287+
self.skipkeys, self.allow_nan, key_memo, self.use_decimal,
288+
self.namedtuple_as_object, self.tuple_as_array)
278289
else:
279290
_iterencode = _make_iterencode(
280291
markers, self.default, _encoder, self.indent, floatstr,
281292
self.key_separator, self.item_separator, self.sort_keys,
282-
self.skipkeys, _one_shot, self.use_decimal)
293+
self.skipkeys, _one_shot, self.use_decimal,
294+
self.namedtuple_as_object, self.tuple_as_array)
283295
try:
284296
return _iterencode(o, 0)
285297
finally:
@@ -315,7 +327,7 @@ def iterencode(self, o, _one_shot=False):
315327

316328
def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
317329
_key_separator, _item_separator, _sort_keys, _skipkeys, _one_shot,
318-
_use_decimal,
330+
_use_decimal, _namedtuple_as_object, _tuple_as_array,
319331
## HACK: hand-optimized bytecode; turn globals into locals
320332
False=False,
321333
True=True,
@@ -373,7 +385,13 @@ def _iterencode_list(lst, _current_indent_level):
373385
yield buf + str(value)
374386
else:
375387
yield buf
376-
if isinstance(value, (list, tuple)):
388+
if isinstance(value, list):
389+
chunks = _iterencode_list(value, _current_indent_level)
390+
elif (_namedtuple_as_object and isinstance(value, tuple) and
391+
hasattr(value, '_asdict')):
392+
chunks = _iterencode_dict(value._asdict(),
393+
_current_indent_level)
394+
elif _tuple_as_array and isinstance(value, tuple):
377395
chunks = _iterencode_list(value, _current_indent_level)
378396
elif isinstance(value, dict):
379397
chunks = _iterencode_dict(value, _current_indent_level)
@@ -452,7 +470,13 @@ def _iterencode_dict(dct, _current_indent_level):
452470
elif _use_decimal and isinstance(value, Decimal):
453471
yield str(value)
454472
else:
455-
if isinstance(value, (list, tuple)):
473+
if isinstance(value, list):
474+
chunks = _iterencode_list(value, _current_indent_level)
475+
elif (_namedtuple_as_object and isinstance(value, tuple) and
476+
hasattr(value, '_asdict')):
477+
chunks = _iterencode_dict(value._asdict(),
478+
_current_indent_level)
479+
elif _tuple_as_array and isinstance(value, tuple):
456480
chunks = _iterencode_list(value, _current_indent_level)
457481
elif isinstance(value, dict):
458482
chunks = _iterencode_dict(value, _current_indent_level)
@@ -480,7 +504,14 @@ def _iterencode(o, _current_indent_level):
480504
yield str(o)
481505
elif isinstance(o, float):
482506
yield _floatstr(o)
483-
elif isinstance(o, (list, tuple)):
507+
elif isinstance(o, list):
508+
for chunk in _iterencode_list(o, _current_indent_level):
509+
yield chunk
510+
elif (_namedtuple_as_object and isinstance(o, tuple) and
511+
hasattr(o, '_asdict')):
512+
for chunk in _iterencode_dict(o._asdict(), _current_indent_level):
513+
yield chunk
514+
elif (_tuple_as_array and isinstance(o, tuple)):
484515
for chunk in _iterencode_list(o, _current_indent_level):
485516
yield chunk
486517
elif isinstance(o, dict):

0 commit comments

Comments
 (0)