simplejson 3.19.3__cp313-cp313-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. simplejson/__init__.py +562 -0
  2. simplejson/_speedups.cp313-win_amd64.pyd +0 -0
  3. simplejson/compat.py +34 -0
  4. simplejson/decoder.py +416 -0
  5. simplejson/encoder.py +740 -0
  6. simplejson/errors.py +53 -0
  7. simplejson/ordered_dict.py +103 -0
  8. simplejson/raw_json.py +9 -0
  9. simplejson/scanner.py +85 -0
  10. simplejson/tests/__init__.py +91 -0
  11. simplejson/tests/_cibw_runner.py +7 -0
  12. simplejson/tests/test_bigint_as_string.py +67 -0
  13. simplejson/tests/test_bitsize_int_as_string.py +73 -0
  14. simplejson/tests/test_check_circular.py +30 -0
  15. simplejson/tests/test_decimal.py +71 -0
  16. simplejson/tests/test_decode.py +127 -0
  17. simplejson/tests/test_default.py +9 -0
  18. simplejson/tests/test_dump.py +249 -0
  19. simplejson/tests/test_encode_basestring_ascii.py +47 -0
  20. simplejson/tests/test_encode_for_html.py +38 -0
  21. simplejson/tests/test_errors.py +68 -0
  22. simplejson/tests/test_fail.py +178 -0
  23. simplejson/tests/test_float.py +38 -0
  24. simplejson/tests/test_for_json.py +97 -0
  25. simplejson/tests/test_indent.py +86 -0
  26. simplejson/tests/test_item_sort_key.py +27 -0
  27. simplejson/tests/test_iterable.py +31 -0
  28. simplejson/tests/test_namedtuple.py +174 -0
  29. simplejson/tests/test_pass1.py +71 -0
  30. simplejson/tests/test_pass2.py +14 -0
  31. simplejson/tests/test_pass3.py +20 -0
  32. simplejson/tests/test_raw_json.py +47 -0
  33. simplejson/tests/test_recursion.py +67 -0
  34. simplejson/tests/test_scanstring.py +200 -0
  35. simplejson/tests/test_separators.py +42 -0
  36. simplejson/tests/test_speedups.py +114 -0
  37. simplejson/tests/test_str_subclass.py +21 -0
  38. simplejson/tests/test_subclass.py +37 -0
  39. simplejson/tests/test_tool.py +114 -0
  40. simplejson/tests/test_tuple.py +47 -0
  41. simplejson/tests/test_unicode.py +154 -0
  42. simplejson/tool.py +42 -0
  43. simplejson-3.19.3.dist-info/LICENSE.txt +79 -0
  44. simplejson-3.19.3.dist-info/METADATA +70 -0
  45. simplejson-3.19.3.dist-info/RECORD +47 -0
  46. simplejson-3.19.3.dist-info/WHEEL +5 -0
  47. simplejson-3.19.3.dist-info/top_level.txt +1 -0
simplejson/__init__.py ADDED
@@ -0,0 +1,562 @@
1
+ r"""JSON (JavaScript Object Notation) <http://json.org> is a subset of
2
+ JavaScript syntax (ECMA-262 3rd edition) used as a lightweight data
3
+ interchange format.
4
+
5
+ :mod:`simplejson` exposes an API familiar to users of the standard library
6
+ :mod:`marshal` and :mod:`pickle` modules. It is the externally maintained
7
+ version of the :mod:`json` library contained in Python 2.6, but maintains
8
+ compatibility back to Python 2.5 and (currently) has significant performance
9
+ advantages, even without using the optional C extension for speedups.
10
+
11
+ Encoding basic Python object hierarchies::
12
+
13
+ >>> import simplejson as json
14
+ >>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
15
+ '["foo", {"bar": ["baz", null, 1.0, 2]}]'
16
+ >>> print(json.dumps("\"foo\bar"))
17
+ "\"foo\bar"
18
+ >>> print(json.dumps(u'\u1234'))
19
+ "\u1234"
20
+ >>> print(json.dumps('\\'))
21
+ "\\"
22
+ >>> print(json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True))
23
+ {"a": 0, "b": 0, "c": 0}
24
+ >>> from simplejson.compat import StringIO
25
+ >>> io = StringIO()
26
+ >>> json.dump(['streaming API'], io)
27
+ >>> io.getvalue()
28
+ '["streaming API"]'
29
+
30
+ Compact encoding::
31
+
32
+ >>> import simplejson as json
33
+ >>> obj = [1,2,3,{'4': 5, '6': 7}]
34
+ >>> json.dumps(obj, separators=(',',':'), sort_keys=True)
35
+ '[1,2,3,{"4":5,"6":7}]'
36
+
37
+ Pretty printing::
38
+
39
+ >>> import simplejson as json
40
+ >>> print(json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=' '))
41
+ {
42
+ "4": 5,
43
+ "6": 7
44
+ }
45
+
46
+ Decoding JSON::
47
+
48
+ >>> import simplejson as json
49
+ >>> obj = [u'foo', {u'bar': [u'baz', None, 1.0, 2]}]
50
+ >>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') == obj
51
+ True
52
+ >>> json.loads('"\\"foo\\bar"') == u'"foo\x08ar'
53
+ True
54
+ >>> from simplejson.compat import StringIO
55
+ >>> io = StringIO('["streaming API"]')
56
+ >>> json.load(io)[0] == 'streaming API'
57
+ True
58
+
59
+ Specializing JSON object decoding::
60
+
61
+ >>> import simplejson as json
62
+ >>> def as_complex(dct):
63
+ ... if '__complex__' in dct:
64
+ ... return complex(dct['real'], dct['imag'])
65
+ ... return dct
66
+ ...
67
+ >>> json.loads('{"__complex__": true, "real": 1, "imag": 2}',
68
+ ... object_hook=as_complex)
69
+ (1+2j)
70
+ >>> from decimal import Decimal
71
+ >>> json.loads('1.1', parse_float=Decimal) == Decimal('1.1')
72
+ True
73
+
74
+ Specializing JSON object encoding::
75
+
76
+ >>> import simplejson as json
77
+ >>> def encode_complex(obj):
78
+ ... if isinstance(obj, complex):
79
+ ... return [obj.real, obj.imag]
80
+ ... raise TypeError('Object of type %s is not JSON serializable' %
81
+ ... obj.__class__.__name__)
82
+ ...
83
+ >>> json.dumps(2 + 1j, default=encode_complex)
84
+ '[2.0, 1.0]'
85
+ >>> json.JSONEncoder(default=encode_complex).encode(2 + 1j)
86
+ '[2.0, 1.0]'
87
+ >>> ''.join(json.JSONEncoder(default=encode_complex).iterencode(2 + 1j))
88
+ '[2.0, 1.0]'
89
+
90
+ Using simplejson.tool from the shell to validate and pretty-print::
91
+
92
+ $ echo '{"json":"obj"}' | python -m simplejson.tool
93
+ {
94
+ "json": "obj"
95
+ }
96
+ $ echo '{ 1.2:3.4}' | python -m simplejson.tool
97
+ Expecting property name: line 1 column 3 (char 2)
98
+
99
+ Parsing multiple documents serialized as JSON lines (newline-delimited JSON)::
100
+
101
+ >>> import simplejson as json
102
+ >>> def loads_lines(docs):
103
+ ... for doc in docs.splitlines():
104
+ ... yield json.loads(doc)
105
+ ...
106
+ >>> sum(doc["count"] for doc in loads_lines('{"count":1}\n{"count":2}\n{"count":3}\n'))
107
+ 6
108
+
109
+ Serializing multiple objects to JSON lines (newline-delimited JSON)::
110
+
111
+ >>> import simplejson as json
112
+ >>> def dumps_lines(objs):
113
+ ... for obj in objs:
114
+ ... yield json.dumps(obj, separators=(',',':')) + '\n'
115
+ ...
116
+ >>> ''.join(dumps_lines([{'count': 1}, {'count': 2}, {'count': 3}]))
117
+ '{"count":1}\n{"count":2}\n{"count":3}\n'
118
+
119
+ """
120
+ from __future__ import absolute_import
121
+ __version__ = '3.19.3'
122
+ __all__ = [
123
+ 'dump', 'dumps', 'load', 'loads',
124
+ 'JSONDecoder', 'JSONDecodeError', 'JSONEncoder',
125
+ 'OrderedDict', 'simple_first', 'RawJSON'
126
+ ]
127
+
128
+ __author__ = 'Bob Ippolito <bob@redivi.com>'
129
+
130
+ from decimal import Decimal
131
+
132
+ from .errors import JSONDecodeError
133
+ from .raw_json import RawJSON
134
+ from .decoder import JSONDecoder
135
+ from .encoder import JSONEncoder, JSONEncoderForHTML
136
+ def _import_OrderedDict():
137
+ import collections
138
+ try:
139
+ return collections.OrderedDict
140
+ except AttributeError:
141
+ from . import ordered_dict
142
+ return ordered_dict.OrderedDict
143
+ OrderedDict = _import_OrderedDict()
144
+
145
+ def _import_c_make_encoder():
146
+ try:
147
+ from ._speedups import make_encoder
148
+ return make_encoder
149
+ except ImportError:
150
+ return None
151
+
152
+ _default_encoder = JSONEncoder()
153
+
154
+ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
155
+ allow_nan=False, cls=None, indent=None, separators=None,
156
+ encoding='utf-8', default=None, use_decimal=True,
157
+ namedtuple_as_object=True, tuple_as_array=True,
158
+ bigint_as_string=False, sort_keys=False, item_sort_key=None,
159
+ for_json=False, ignore_nan=False, int_as_string_bitcount=None,
160
+ iterable_as_array=False, **kw):
161
+ """Serialize ``obj`` as a JSON formatted stream to ``fp`` (a
162
+ ``.write()``-supporting file-like object).
163
+
164
+ If *skipkeys* is true then ``dict`` keys that are not basic types
165
+ (``str``, ``int``, ``long``, ``float``, ``bool``, ``None``)
166
+ will be skipped instead of raising a ``TypeError``.
167
+
168
+ If *ensure_ascii* is false (default: ``True``), then the output may
169
+ contain non-ASCII characters, so long as they do not need to be escaped
170
+ by JSON. When it is true, all non-ASCII characters are escaped.
171
+
172
+ If *allow_nan* is true (default: ``False``), then out of range ``float``
173
+ values (``nan``, ``inf``, ``-inf``) will be serialized to
174
+ their JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``)
175
+ instead of raising a ValueError. See
176
+ *ignore_nan* for ECMA-262 compliant behavior.
177
+
178
+ If *indent* is a string, then JSON array elements and object members
179
+ will be pretty-printed with a newline followed by that string repeated
180
+ for each level of nesting. ``None`` (the default) selects the most compact
181
+ representation without any newlines.
182
+
183
+ If specified, *separators* should be an
184
+ ``(item_separator, key_separator)`` tuple. The default is ``(', ', ': ')``
185
+ if *indent* is ``None`` and ``(',', ': ')`` otherwise. To get the most
186
+ compact JSON representation, you should specify ``(',', ':')`` to eliminate
187
+ whitespace.
188
+
189
+ *encoding* is the character encoding for str instances, default is UTF-8.
190
+
191
+ *default(obj)* is a function that should return a serializable version
192
+ of obj or raise ``TypeError``. The default simply raises ``TypeError``.
193
+
194
+ If *use_decimal* is true (default: ``True``) then decimal.Decimal
195
+ will be natively serialized to JSON with full precision.
196
+
197
+ If *namedtuple_as_object* is true (default: ``True``),
198
+ :class:`tuple` subclasses with ``_asdict()`` methods will be encoded
199
+ as JSON objects.
200
+
201
+ If *tuple_as_array* is true (default: ``True``),
202
+ :class:`tuple` (and subclasses) will be encoded as JSON arrays.
203
+
204
+ If *iterable_as_array* is true (default: ``False``),
205
+ any object not in the above table that implements ``__iter__()``
206
+ will be encoded as a JSON array.
207
+
208
+ If *bigint_as_string* is true (default: ``False``), ints 2**53 and higher
209
+ or lower than -2**53 will be encoded as strings. This is to avoid the
210
+ rounding that happens in Javascript otherwise. Note that this is still a
211
+ lossy operation that will not round-trip correctly and should be used
212
+ sparingly.
213
+
214
+ If *int_as_string_bitcount* is a positive number (n), then int of size
215
+ greater than or equal to 2**n or lower than or equal to -2**n will be
216
+ encoded as strings.
217
+
218
+ If specified, *item_sort_key* is a callable used to sort the items in
219
+ each dictionary. This is useful if you want to sort items other than
220
+ in alphabetical order by key. This option takes precedence over
221
+ *sort_keys*.
222
+
223
+ If *sort_keys* is true (default: ``False``), the output of dictionaries
224
+ will be sorted by item.
225
+
226
+ If *for_json* is true (default: ``False``), objects with a ``for_json()``
227
+ method will use the return value of that method for encoding as JSON
228
+ instead of the object.
229
+
230
+ If *ignore_nan* is true (default: ``False``), then out of range
231
+ :class:`float` values (``nan``, ``inf``, ``-inf``) will be serialized as
232
+ ``null`` in compliance with the ECMA-262 specification. If true, this will
233
+ override *allow_nan*.
234
+
235
+ To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
236
+ ``.default()`` method to serialize additional types), specify it with
237
+ the ``cls`` kwarg. NOTE: You should use *default* or *for_json* instead
238
+ of subclassing whenever possible.
239
+
240
+ """
241
+ # cached encoder
242
+ if (not skipkeys and ensure_ascii and
243
+ check_circular and not allow_nan and
244
+ cls is None and indent is None and separators is None and
245
+ encoding == 'utf-8' and default is None and use_decimal
246
+ and namedtuple_as_object and tuple_as_array and not iterable_as_array
247
+ and not bigint_as_string and not sort_keys
248
+ and not item_sort_key and not for_json
249
+ and not ignore_nan and int_as_string_bitcount is None
250
+ and not kw
251
+ ):
252
+ iterable = _default_encoder.iterencode(obj)
253
+ else:
254
+ if cls is None:
255
+ cls = JSONEncoder
256
+ iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii,
257
+ check_circular=check_circular, allow_nan=allow_nan, indent=indent,
258
+ separators=separators, encoding=encoding,
259
+ default=default, use_decimal=use_decimal,
260
+ namedtuple_as_object=namedtuple_as_object,
261
+ tuple_as_array=tuple_as_array,
262
+ iterable_as_array=iterable_as_array,
263
+ bigint_as_string=bigint_as_string,
264
+ sort_keys=sort_keys,
265
+ item_sort_key=item_sort_key,
266
+ for_json=for_json,
267
+ ignore_nan=ignore_nan,
268
+ int_as_string_bitcount=int_as_string_bitcount,
269
+ **kw).iterencode(obj)
270
+ # could accelerate with writelines in some versions of Python, at
271
+ # a debuggability cost
272
+ for chunk in iterable:
273
+ fp.write(chunk)
274
+
275
+
276
+ def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
277
+ allow_nan=False, cls=None, indent=None, separators=None,
278
+ encoding='utf-8', default=None, use_decimal=True,
279
+ namedtuple_as_object=True, tuple_as_array=True,
280
+ bigint_as_string=False, sort_keys=False, item_sort_key=None,
281
+ for_json=False, ignore_nan=False, int_as_string_bitcount=None,
282
+ iterable_as_array=False, **kw):
283
+ """Serialize ``obj`` to a JSON formatted ``str``.
284
+
285
+ If ``skipkeys`` is true then ``dict`` keys that are not basic types
286
+ (``str``, ``int``, ``long``, ``float``, ``bool``, ``None``)
287
+ will be skipped instead of raising a ``TypeError``.
288
+
289
+ If *ensure_ascii* is false (default: ``True``), then the output may
290
+ contain non-ASCII characters, so long as they do not need to be escaped
291
+ by JSON. When it is true, all non-ASCII characters are escaped.
292
+
293
+ If ``check_circular`` is false, then the circular reference check
294
+ for container types will be skipped and a circular reference will
295
+ result in an ``OverflowError`` (or worse).
296
+
297
+ If *allow_nan* is true (default: ``False``), then out of range ``float``
298
+ values (``nan``, ``inf``, ``-inf``) will be serialized to
299
+ their JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``)
300
+ instead of raising a ValueError. See
301
+ *ignore_nan* for ECMA-262 compliant behavior.
302
+
303
+ If ``indent`` is a string, then JSON array elements and object members
304
+ will be pretty-printed with a newline followed by that string repeated
305
+ for each level of nesting. ``None`` (the default) selects the most compact
306
+ representation without any newlines. For backwards compatibility with
307
+ versions of simplejson earlier than 2.1.0, an integer is also accepted
308
+ and is converted to a string with that many spaces.
309
+
310
+ If specified, ``separators`` should be an
311
+ ``(item_separator, key_separator)`` tuple. The default is ``(', ', ': ')``
312
+ if *indent* is ``None`` and ``(',', ': ')`` otherwise. To get the most
313
+ compact JSON representation, you should specify ``(',', ':')`` to eliminate
314
+ whitespace.
315
+
316
+ ``encoding`` is the character encoding for bytes instances, default is
317
+ UTF-8.
318
+
319
+ ``default(obj)`` is a function that should return a serializable version
320
+ of obj or raise TypeError. The default simply raises TypeError.
321
+
322
+ If *use_decimal* is true (default: ``True``) then decimal.Decimal
323
+ will be natively serialized to JSON with full precision.
324
+
325
+ If *namedtuple_as_object* is true (default: ``True``),
326
+ :class:`tuple` subclasses with ``_asdict()`` methods will be encoded
327
+ as JSON objects.
328
+
329
+ If *tuple_as_array* is true (default: ``True``),
330
+ :class:`tuple` (and subclasses) will be encoded as JSON arrays.
331
+
332
+ If *iterable_as_array* is true (default: ``False``),
333
+ any object not in the above table that implements ``__iter__()``
334
+ will be encoded as a JSON array.
335
+
336
+ If *bigint_as_string* is true (not the default), ints 2**53 and higher
337
+ or lower than -2**53 will be encoded as strings. This is to avoid the
338
+ rounding that happens in Javascript otherwise.
339
+
340
+ If *int_as_string_bitcount* is a positive number (n), then int of size
341
+ greater than or equal to 2**n or lower than or equal to -2**n will be
342
+ encoded as strings.
343
+
344
+ If specified, *item_sort_key* is a callable used to sort the items in
345
+ each dictionary. This is useful if you want to sort items other than
346
+ in alphabetical order by key. This option takes precedence over
347
+ *sort_keys*.
348
+
349
+ If *sort_keys* is true (default: ``False``), the output of dictionaries
350
+ will be sorted by item.
351
+
352
+ If *for_json* is true (default: ``False``), objects with a ``for_json()``
353
+ method will use the return value of that method for encoding as JSON
354
+ instead of the object.
355
+
356
+ If *ignore_nan* is true (default: ``False``), then out of range
357
+ :class:`float` values (``nan``, ``inf``, ``-inf``) will be serialized as
358
+ ``null`` in compliance with the ECMA-262 specification. If true, this will
359
+ override *allow_nan*.
360
+
361
+ To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
362
+ ``.default()`` method to serialize additional types), specify it with
363
+ the ``cls`` kwarg. NOTE: You should use *default* instead of subclassing
364
+ whenever possible.
365
+
366
+ """
367
+ # cached encoder
368
+ if (not skipkeys and ensure_ascii and
369
+ check_circular and not allow_nan and
370
+ cls is None and indent is None and separators is None and
371
+ encoding == 'utf-8' and default is None and use_decimal
372
+ and namedtuple_as_object and tuple_as_array and not iterable_as_array
373
+ and not bigint_as_string and not sort_keys
374
+ and not item_sort_key and not for_json
375
+ and not ignore_nan and int_as_string_bitcount is None
376
+ and not kw
377
+ ):
378
+ return _default_encoder.encode(obj)
379
+ if cls is None:
380
+ cls = JSONEncoder
381
+ return cls(
382
+ skipkeys=skipkeys, ensure_ascii=ensure_ascii,
383
+ check_circular=check_circular, allow_nan=allow_nan, indent=indent,
384
+ separators=separators, encoding=encoding, default=default,
385
+ use_decimal=use_decimal,
386
+ namedtuple_as_object=namedtuple_as_object,
387
+ tuple_as_array=tuple_as_array,
388
+ iterable_as_array=iterable_as_array,
389
+ bigint_as_string=bigint_as_string,
390
+ sort_keys=sort_keys,
391
+ item_sort_key=item_sort_key,
392
+ for_json=for_json,
393
+ ignore_nan=ignore_nan,
394
+ int_as_string_bitcount=int_as_string_bitcount,
395
+ **kw).encode(obj)
396
+
397
+
398
+ _default_decoder = JSONDecoder()
399
+
400
+
401
+ def load(fp, encoding=None, cls=None, object_hook=None, parse_float=None,
402
+ parse_int=None, parse_constant=None, object_pairs_hook=None,
403
+ use_decimal=False, allow_nan=False, **kw):
404
+ """Deserialize ``fp`` (a ``.read()``-supporting file-like object containing
405
+ a JSON document as `str` or `bytes`) to a Python object.
406
+
407
+ *encoding* determines the encoding used to interpret any
408
+ `bytes` objects decoded by this instance (``'utf-8'`` by
409
+ default). It has no effect when decoding `str` objects.
410
+
411
+ *object_hook*, if specified, will be called with the result of every
412
+ JSON object decoded and its return value will be used in place of the
413
+ given :class:`dict`. This can be used to provide custom
414
+ deserializations (e.g. to support JSON-RPC class hinting).
415
+
416
+ *object_pairs_hook* is an optional function that will be called with
417
+ the result of any object literal decode with an ordered list of pairs.
418
+ The return value of *object_pairs_hook* will be used instead of the
419
+ :class:`dict`. This feature can be used to implement custom decoders
420
+ that rely on the order that the key and value pairs are decoded (for
421
+ example, :func:`collections.OrderedDict` will remember the order of
422
+ insertion). If *object_hook* is also defined, the *object_pairs_hook*
423
+ takes priority.
424
+
425
+ *parse_float*, if specified, will be called with the string of every
426
+ JSON float to be decoded. By default, this is equivalent to
427
+ ``float(num_str)``. This can be used to use another datatype or parser
428
+ for JSON floats (e.g. :class:`decimal.Decimal`).
429
+
430
+ *parse_int*, if specified, will be called with the string of every
431
+ JSON int to be decoded. By default, this is equivalent to
432
+ ``int(num_str)``. This can be used to use another datatype or parser
433
+ for JSON integers (e.g. :class:`float`).
434
+
435
+ *allow_nan*, if True (default false), will allow the parser to
436
+ accept the non-standard floats ``NaN``, ``Infinity``, and ``-Infinity``
437
+ and enable the use of the deprecated *parse_constant*.
438
+
439
+ If *use_decimal* is true (default: ``False``) then it implies
440
+ parse_float=decimal.Decimal for parity with ``dump``.
441
+
442
+ *parse_constant*, if specified, will be
443
+ called with one of the following strings: ``'-Infinity'``,
444
+ ``'Infinity'``, ``'NaN'``. It is not recommended to use this feature,
445
+ as it is rare to parse non-compliant JSON containing these values.
446
+
447
+ To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
448
+ kwarg. NOTE: You should use *object_hook* or *object_pairs_hook* instead
449
+ of subclassing whenever possible.
450
+
451
+ """
452
+ return loads(fp.read(),
453
+ encoding=encoding, cls=cls, object_hook=object_hook,
454
+ parse_float=parse_float, parse_int=parse_int,
455
+ parse_constant=parse_constant, object_pairs_hook=object_pairs_hook,
456
+ use_decimal=use_decimal, allow_nan=allow_nan, **kw)
457
+
458
+
459
+ def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None,
460
+ parse_int=None, parse_constant=None, object_pairs_hook=None,
461
+ use_decimal=False, allow_nan=False, **kw):
462
+ """Deserialize ``s`` (a ``str`` or ``unicode`` instance containing a JSON
463
+ document) to a Python object.
464
+
465
+ *encoding* determines the encoding used to interpret any
466
+ :class:`bytes` objects decoded by this instance (``'utf-8'`` by
467
+ default). It has no effect when decoding :class:`unicode` objects.
468
+
469
+ *object_hook*, if specified, will be called with the result of every
470
+ JSON object decoded and its return value will be used in place of the
471
+ given :class:`dict`. This can be used to provide custom
472
+ deserializations (e.g. to support JSON-RPC class hinting).
473
+
474
+ *object_pairs_hook* is an optional function that will be called with
475
+ the result of any object literal decode with an ordered list of pairs.
476
+ The return value of *object_pairs_hook* will be used instead of the
477
+ :class:`dict`. This feature can be used to implement custom decoders
478
+ that rely on the order that the key and value pairs are decoded (for
479
+ example, :func:`collections.OrderedDict` will remember the order of
480
+ insertion). If *object_hook* is also defined, the *object_pairs_hook*
481
+ takes priority.
482
+
483
+ *parse_float*, if specified, will be called with the string of every
484
+ JSON float to be decoded. By default, this is equivalent to
485
+ ``float(num_str)``. This can be used to use another datatype or parser
486
+ for JSON floats (e.g. :class:`decimal.Decimal`).
487
+
488
+ *parse_int*, if specified, will be called with the string of every
489
+ JSON int to be decoded. By default, this is equivalent to
490
+ ``int(num_str)``. This can be used to use another datatype or parser
491
+ for JSON integers (e.g. :class:`float`).
492
+
493
+ *allow_nan*, if True (default false), will allow the parser to
494
+ accept the non-standard floats ``NaN``, ``Infinity``, and ``-Infinity``
495
+ and enable the use of the deprecated *parse_constant*.
496
+
497
+ If *use_decimal* is true (default: ``False``) then it implies
498
+ parse_float=decimal.Decimal for parity with ``dump``.
499
+
500
+ *parse_constant*, if specified, will be
501
+ called with one of the following strings: ``'-Infinity'``,
502
+ ``'Infinity'``, ``'NaN'``. It is not recommended to use this feature,
503
+ as it is rare to parse non-compliant JSON containing these values.
504
+
505
+ To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
506
+ kwarg. NOTE: You should use *object_hook* or *object_pairs_hook* instead
507
+ of subclassing whenever possible.
508
+
509
+ """
510
+ if (cls is None and encoding is None and object_hook is None and
511
+ parse_int is None and parse_float is None and
512
+ parse_constant is None and object_pairs_hook is None
513
+ and not use_decimal and not allow_nan and not kw):
514
+ return _default_decoder.decode(s)
515
+ if cls is None:
516
+ cls = JSONDecoder
517
+ if object_hook is not None:
518
+ kw['object_hook'] = object_hook
519
+ if object_pairs_hook is not None:
520
+ kw['object_pairs_hook'] = object_pairs_hook
521
+ if parse_float is not None:
522
+ kw['parse_float'] = parse_float
523
+ if parse_int is not None:
524
+ kw['parse_int'] = parse_int
525
+ if parse_constant is not None:
526
+ kw['parse_constant'] = parse_constant
527
+ if use_decimal:
528
+ if parse_float is not None:
529
+ raise TypeError("use_decimal=True implies parse_float=Decimal")
530
+ kw['parse_float'] = Decimal
531
+ if allow_nan:
532
+ kw['allow_nan'] = True
533
+ return cls(encoding=encoding, **kw).decode(s)
534
+
535
+
536
+ def _toggle_speedups(enabled):
537
+ from . import decoder as dec
538
+ from . import encoder as enc
539
+ from . import scanner as scan
540
+ c_make_encoder = _import_c_make_encoder()
541
+ if enabled:
542
+ dec.scanstring = dec.c_scanstring or dec.py_scanstring
543
+ enc.c_make_encoder = c_make_encoder
544
+ enc.encode_basestring_ascii = (enc.c_encode_basestring_ascii or
545
+ enc.py_encode_basestring_ascii)
546
+ scan.make_scanner = scan.c_make_scanner or scan.py_make_scanner
547
+ else:
548
+ dec.scanstring = dec.py_scanstring
549
+ enc.c_make_encoder = None
550
+ enc.encode_basestring_ascii = enc.py_encode_basestring_ascii
551
+ scan.make_scanner = scan.py_make_scanner
552
+ dec.make_scanner = scan.make_scanner
553
+ global _default_decoder
554
+ _default_decoder = JSONDecoder()
555
+ global _default_encoder
556
+ _default_encoder = JSONEncoder()
557
+
558
+ def simple_first(kv):
559
+ """Helper function to pass to item_sort_key to sort simple
560
+ elements to the top, then container elements.
561
+ """
562
+ return (isinstance(kv[1], (list, dict, tuple)), kv[0])
Binary file
simplejson/compat.py ADDED
@@ -0,0 +1,34 @@
1
+ """Python 3 compatibility shims
2
+ """
3
+ import sys
4
+ if sys.version_info[0] < 3:
5
+ PY3 = False
6
+ def b(s):
7
+ return s
8
+ try:
9
+ from cStringIO import StringIO
10
+ except ImportError:
11
+ from StringIO import StringIO
12
+ BytesIO = StringIO
13
+ text_type = unicode
14
+ binary_type = str
15
+ string_types = (basestring,)
16
+ integer_types = (int, long)
17
+ unichr = unichr
18
+ reload_module = reload
19
+ else:
20
+ PY3 = True
21
+ if sys.version_info[:2] >= (3, 4):
22
+ from importlib import reload as reload_module
23
+ else:
24
+ from imp import reload as reload_module
25
+ def b(s):
26
+ return bytes(s, 'latin1')
27
+ from io import StringIO, BytesIO
28
+ text_type = str
29
+ binary_type = bytes
30
+ string_types = (str,)
31
+ integer_types = (int,)
32
+ unichr = chr
33
+
34
+ long_type = integer_types[-1]