1 """Implementation of JSONEncoder
3 from __future__
import absolute_import
5 from operator
import itemgetter
8 from .compat
import unichr, binary_type, text_type, string_types, integer_types, PY3
11 from .
import _speedups
12 return _speedups.encode_basestring_ascii, _speedups.make_encoder
17 from .decoder
import PosInf
18 from .raw_json
import RawJSON
20 ESCAPE = re.compile(
r'[\x00-\x1f\\"]')
21 ESCAPE_ASCII = re.compile(
r'([\\"]|[^\ -~])')
22 HAS_UTF8 = re.compile(
r'[\x80-\xff]')
34 ESCAPE_DCT.setdefault(chr(i),
'\\u%04x' % (i,))
39 """Return a JSON representation of a Python string
43 if isinstance(s, bytes):
45 elif type(s)
is not str:
50 if isinstance(s, str)
and HAS_UTF8.search(s)
is not None:
51 s = unicode(s,
'utf-8')
52 elif type(s)
not in (str, unicode):
56 if isinstance(s, str):
59 s = unicode.__getnewargs__(s)[0]
61 return ESCAPE_DCT[match.group(0)]
62 return _q + ESCAPE.sub(replace, s) + _q
66 """Return an ASCII-only JSON representation of a Python string
70 if isinstance(s, bytes):
72 elif type(s)
is not str:
77 if isinstance(s, str)
and HAS_UTF8.search(s)
is not None:
78 s = unicode(s,
'utf-8')
79 elif type(s)
not in (str, unicode):
83 if isinstance(s, str):
86 s = unicode.__getnewargs__(s)[0]
95 return '\\u%04x' % (n,)
99 s1 = 0xd800 | ((n >> 10) & 0x3ff)
100 s2 = 0xdc00 | (n & 0x3ff)
102 return '\\u%04x\\u%04x' % (s1, s2)
103 return '"' + str(ESCAPE_ASCII.sub(replace, s)) +
'"'
106 encode_basestring_ascii = (
107 c_encode_basestring_ascii
or py_encode_basestring_ascii)
110 """Extensible JSON <http://json.org> encoder for Python data structures.
112 Supports the following objects and types by default:
114 +-------------------+---------------+
116 +===================+===============+
117 | dict, namedtuple | object |
118 +-------------------+---------------+
119 | list, tuple | array |
120 +-------------------+---------------+
121 | str, unicode | string |
122 +-------------------+---------------+
123 | int, long, float | number |
124 +-------------------+---------------+
126 +-------------------+---------------+
128 +-------------------+---------------+
130 +-------------------+---------------+
132 To extend this to recognize other objects, subclass and implement a
133 ``.default()`` method with another method that returns a serializable
134 object for ``o`` if possible, otherwise it should call the superclass
135 implementation (to raise ``TypeError``).
138 item_separator =
', '
141 def __init__(self, skipkeys=False, ensure_ascii=True,
142 check_circular=True, allow_nan=True, sort_keys=False,
143 indent=None, separators=None, encoding='utf-8', default=None,
144 use_decimal=True, namedtuple_as_object=True,
145 tuple_as_array=True, bigint_as_string=False,
146 item_sort_key=None, for_json=False, ignore_nan=False,
147 int_as_string_bitcount=None, iterable_as_array=False):
148 """Constructor for JSONEncoder, with sensible defaults.
150 If skipkeys is false, then it is a TypeError to attempt
151 encoding of keys that are not str, int, long, float or None. If
152 skipkeys is True, such items are simply skipped.
154 If ensure_ascii is true, the output is guaranteed to be str
155 objects with all incoming unicode characters escaped. If
156 ensure_ascii is false, the output will be unicode object.
158 If check_circular is true, then lists, dicts, and custom encoded
159 objects will be checked for circular references during encoding to
160 prevent an infinite recursion (which would cause an OverflowError).
161 Otherwise, no such check takes place.
163 If allow_nan is true, then NaN, Infinity, and -Infinity will be
164 encoded as such. This behavior is not JSON specification compliant,
165 but is consistent with most JavaScript based encoders and decoders.
166 Otherwise, it will be a ValueError to encode such floats.
168 If sort_keys is true, then the output of dictionaries will be
169 sorted by key; this is useful for regression tests to ensure
170 that JSON serializations can be compared on a day-to-day basis.
172 If indent is a string, then JSON array elements and object members
173 will be pretty-printed with a newline followed by that string repeated
174 for each level of nesting. ``None`` (the default) selects the most compact
175 representation without any newlines. For backwards compatibility with
176 versions of simplejson earlier than 2.1.0, an integer is also accepted
177 and is converted to a string with that many spaces.
179 If specified, separators should be an (item_separator, key_separator)
180 tuple. The default is (', ', ': ') if *indent* is ``None`` and
181 (',', ': ') otherwise. To get the most compact JSON representation,
182 you should specify (',', ':') to eliminate whitespace.
184 If specified, default is a function that gets called for objects
185 that can't otherwise be serialized. It should return a JSON encodable
186 version of the object or raise a ``TypeError``.
188 If encoding is not None, then all input strings will be
189 transformed into unicode using that encoding prior to JSON-encoding.
190 The default is UTF-8.
192 If use_decimal is true (default: ``True``), ``decimal.Decimal`` will
193 be supported directly by the encoder. For the inverse, decode JSON
194 with ``parse_float=decimal.Decimal``.
196 If namedtuple_as_object is true (the default), objects with
197 ``_asdict()`` methods will be encoded as JSON objects.
199 If tuple_as_array is true (the default), tuple (and subclasses) will
200 be encoded as JSON arrays.
202 If *iterable_as_array* is true (default: ``False``),
203 any object not in the above table that implements ``__iter__()``
204 will be encoded as a JSON array.
206 If bigint_as_string is true (not the default), ints 2**53 and higher
207 or lower than -2**53 will be encoded as strings. This is to avoid the
208 rounding that happens in Javascript otherwise.
210 If int_as_string_bitcount is a positive number (n), then int of size
211 greater than or equal to 2**n or lower than or equal to -2**n will be
214 If specified, item_sort_key is a callable used to sort the items in
215 each dictionary. This is useful if you want to sort items other than
216 in alphabetical order by key.
218 If for_json is true (not the default), objects with a ``for_json()``
219 method will use the return value of that method for encoding as JSON
220 instead of the object.
222 If *ignore_nan* is true (default: ``False``), then out of range
223 :class:`float` values (``nan``, ``inf``, ``-inf``) will be serialized
224 as ``null`` in compliance with the ECMA-262 specification. If true,
225 this will override *allow_nan*.
243 if indent
is not None and not isinstance(indent, string_types):
244 indent = indent *
' '
246 if separators
is not None:
248 elif indent
is not None:
250 if default
is not None:
255 """Implement this method in a subclass such that it returns
256 a serializable object for ``o``, or calls the base implementation
257 (to raise a ``TypeError``).
259 For example, to support arbitrary iterators, you could
260 implement default like this::
262 def default(self, o):
268 return list(iterable)
269 return JSONEncoder.default(self, o)
272 raise TypeError(
'Object of type %s is not JSON serializable' %
273 o.__class__.__name__)
276 """Return a JSON string representation of a Python data structure.
278 >>> from simplejson import JSONEncoder
279 >>> JSONEncoder().encode({"foo": ["bar", "baz"]})
280 '{"foo": ["bar", "baz"]}'
284 if isinstance(o, binary_type):
286 if (_encoding
is not None and not (_encoding ==
'utf-8')):
288 if isinstance(o, string_types):
297 if not isinstance(chunks, (list, tuple)):
298 chunks = list(chunks)
300 return ''.join(chunks)
302 return u''.join(chunks)
305 """Encode the given object and yield each string
306 representation as available.
310 for chunk in JSONEncoder().iterencode(bigobject):
311 mysocket.write(chunk)
319 _encoder = encode_basestring_ascii
321 _encoder = encode_basestring
323 def _encoder(o, _orig_encoder=_encoder, _encoding=self.encoding):
324 if isinstance(o, binary_type):
326 return _orig_encoder(o)
328 def floatstr(o, allow_nan=self.allow_nan, ignore_nan=self.ignore_nan,
329 _repr=FLOAT_REPR, _inf=PosInf, _neginf=-PosInf):
350 "Out of range float values are not JSON compliant: " +
356 int_as_string_bitcount = (
358 if (_one_shot
and c_make_encoder
is not None
360 _iterencode = c_make_encoder(
365 int_as_string_bitcount,
374 int_as_string_bitcount,
378 return _iterencode(o, 0)
384 """An encoder that produces JSON safe to embed in HTML.
386 To embed JSON content in, say, a script tag on a web page, the
387 characters &, < and > should be escaped. They cannot be escaped
388 with the usual entities (e.g. &) because they are not expanded
389 within <script> tags.
391 This class also escapes the line separator and paragraph separator
392 characters U+2028 and U+2029, irrespective of the ensure_ascii setting,
393 as these characters are not valid in JavaScript strings (see
394 http://timelessrepo.com/json-isnt-a-javascript-subset).
402 return ''.join(chunks)
404 return u''.join(chunks)
407 chunks = super(JSONEncoderForHTML, self).
iterencode(o, _one_shot)
409 chunk = chunk.replace(
'&',
'\\u0026')
410 chunk = chunk.replace(
'<',
'\\u003c')
411 chunk = chunk.replace(
'>',
'\\u003e')
414 chunk = chunk.replace(
u'\u2028',
'\\u2028')
415 chunk = chunk.replace(
u'\u2029',
'\\u2029')
421 _key_separator, _item_separator, _sort_keys, _skipkeys, _one_shot,
422 _use_decimal, _namedtuple_as_object, _tuple_as_array,
423 _int_as_string_bitcount, _item_sort_key,
428 ValueError=ValueError,
429 string_types=string_types,
434 integer_types=integer_types,
435 isinstance=isinstance,
441 if _use_decimal
and Decimal
is None:
442 Decimal = decimal.Decimal
443 if _item_sort_key
and not callable(_item_sort_key):
444 raise TypeError(
"item_sort_key must be None or callable")
445 elif _sort_keys
and not _item_sort_key:
446 _item_sort_key = itemgetter(0)
448 if (_int_as_string_bitcount
is not None and
449 (_int_as_string_bitcount <= 0
or
450 not isinstance(_int_as_string_bitcount, integer_types))):
451 raise TypeError(
"int_as_string_bitcount must be a positive integer")
453 def _encode_int(value):
455 _int_as_string_bitcount
is None
457 _int_as_string_bitcount < 1
459 if type(value)
not in integer_types:
464 (-1 << _int_as_string_bitcount)
466 (1 << _int_as_string_bitcount)
469 return '"' + str(value) +
'"'
471 def _iterencode_list(lst, _current_indent_level):
475 if markers
is not None:
477 if markerid
in markers:
478 raise ValueError(
"Circular reference detected")
479 markers[markerid] = lst
481 if _indent
is not None:
482 _current_indent_level += 1
483 newline_indent =
'\n' + (_indent * _current_indent_level)
484 separator = _item_separator + newline_indent
485 buf += newline_indent
487 newline_indent =
None
488 separator = _item_separator
495 if isinstance(value, string_types):
496 yield buf + _encoder(value)
497 elif _PY3
and isinstance(value, bytes)
and _encoding
is not None:
498 yield buf + _encoder(value)
499 elif isinstance(value, RawJSON):
500 yield buf + value.encoded_json
507 elif isinstance(value, integer_types):
508 yield buf + _encode_int(value)
509 elif isinstance(value, float):
510 yield buf + _floatstr(value)
511 elif _use_decimal
and isinstance(value, Decimal):
512 yield buf + str(value)
515 for_json = _for_json
and getattr(value,
'for_json',
None)
516 if for_json
and callable(for_json):
517 chunks = _iterencode(for_json(), _current_indent_level)
518 elif isinstance(value, list):
519 chunks = _iterencode_list(value, _current_indent_level)
521 _asdict = _namedtuple_as_object
and getattr(value,
'_asdict',
None)
522 if _asdict
and callable(_asdict):
523 chunks = _iterencode_dict(_asdict(),
524 _current_indent_level)
525 elif _tuple_as_array
and isinstance(value, tuple):
526 chunks = _iterencode_list(value, _current_indent_level)
527 elif isinstance(value, dict):
528 chunks = _iterencode_dict(value, _current_indent_level)
530 chunks = _iterencode(value, _current_indent_level)
537 if newline_indent
is not None:
538 _current_indent_level -= 1
539 yield '\n' + (_indent * _current_indent_level)
541 if markers
is not None:
542 del markers[markerid]
544 def _stringify_key(key):
545 if isinstance(key, string_types):
547 elif _PY3
and isinstance(key, bytes)
and _encoding
is not None:
548 key = str(key, _encoding)
549 elif isinstance(key, float):
557 elif isinstance(key, integer_types):
558 if type(key)
not in integer_types:
562 elif _use_decimal
and isinstance(key, Decimal):
567 raise TypeError(
'keys must be str, int, float, bool or None, '
568 'not %s' % key.__class__.__name__)
571 def _iterencode_dict(dct, _current_indent_level):
575 if markers
is not None:
577 if markerid
in markers:
578 raise ValueError(
"Circular reference detected")
579 markers[markerid] = dct
581 if _indent
is not None:
582 _current_indent_level += 1
583 newline_indent =
'\n' + (_indent * _current_indent_level)
584 item_separator = _item_separator + newline_indent
587 newline_indent =
None
588 item_separator = _item_separator
591 iteritems = dct.items()
593 iteritems = dct.iteritems()
596 for k, v
in dct.items():
597 if not isinstance(k, string_types):
598 k = _stringify_key(k)
602 items.sort(key=_item_sort_key)
605 for key, value
in items:
606 if not (_item_sort_key
or isinstance(key, string_types)):
607 key = _stringify_key(key)
617 if isinstance(value, string_types):
618 yield _encoder(value)
619 elif _PY3
and isinstance(value, bytes)
and _encoding
is not None:
620 yield _encoder(value)
621 elif isinstance(value, RawJSON):
622 yield value.encoded_json
629 elif isinstance(value, integer_types):
630 yield _encode_int(value)
631 elif isinstance(value, float):
632 yield _floatstr(value)
633 elif _use_decimal
and isinstance(value, Decimal):
636 for_json = _for_json
and getattr(value,
'for_json',
None)
637 if for_json
and callable(for_json):
638 chunks = _iterencode(for_json(), _current_indent_level)
639 elif isinstance(value, list):
640 chunks = _iterencode_list(value, _current_indent_level)
642 _asdict = _namedtuple_as_object
and getattr(value,
'_asdict',
None)
643 if _asdict
and callable(_asdict):
644 chunks = _iterencode_dict(_asdict(),
645 _current_indent_level)
646 elif _tuple_as_array
and isinstance(value, tuple):
647 chunks = _iterencode_list(value, _current_indent_level)
648 elif isinstance(value, dict):
649 chunks = _iterencode_dict(value, _current_indent_level)
651 chunks = _iterencode(value, _current_indent_level)
654 if newline_indent
is not None:
655 _current_indent_level -= 1
656 yield '\n' + (_indent * _current_indent_level)
658 if markers
is not None:
659 del markers[markerid]
661 def _iterencode(o, _current_indent_level):
662 if isinstance(o, string_types):
664 elif _PY3
and isinstance(o, bytes)
and _encoding
is not None:
666 elif isinstance(o, RawJSON):
674 elif isinstance(o, integer_types):
676 elif isinstance(o, float):
679 for_json = _for_json
and getattr(o,
'for_json',
None)
680 if for_json
and callable(for_json):
681 for chunk
in _iterencode(for_json(), _current_indent_level):
683 elif isinstance(o, list):
684 for chunk
in _iterencode_list(o, _current_indent_level):
687 _asdict = _namedtuple_as_object
and getattr(o,
'_asdict',
None)
688 if _asdict
and callable(_asdict):
689 for chunk
in _iterencode_dict(_asdict(),
690 _current_indent_level):
692 elif (_tuple_as_array
and isinstance(o, tuple)):
693 for chunk
in _iterencode_list(o, _current_indent_level):
695 elif isinstance(o, dict):
696 for chunk
in _iterencode_dict(o, _current_indent_level):
698 elif _use_decimal
and isinstance(o, Decimal):
701 while _iterable_as_array:
708 for chunk
in _iterencode_list(o, _current_indent_level):
711 if markers
is not None:
713 if markerid
in markers:
714 raise ValueError(
"Circular reference detected")
715 markers[markerid] = o
717 for chunk
in _iterencode(o, _current_indent_level):
719 if markers
is not None:
720 del markers[markerid]