Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 1 | """Implementation of JSONEncoder |
| 2 | """ |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 3 | import re |
| 4 | |
| 5 | try: |
| 6 | from _json import encode_basestring_ascii as c_encode_basestring_ascii |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 7 | except ImportError: |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 8 | c_encode_basestring_ascii = None |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 9 | try: |
Antoine Pitrou | dc3eaa8 | 2015-01-11 16:41:01 +0100 | [diff] [blame] | 10 | from _json import encode_basestring as c_encode_basestring |
| 11 | except ImportError: |
| 12 | c_encode_basestring = None |
| 13 | try: |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 14 | from _json import make_encoder as c_make_encoder |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 15 | except ImportError: |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 16 | c_make_encoder = None |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 17 | |
| 18 | ESCAPE = re.compile(r'[\x00-\x1f\\"\b\f\n\r\t]') |
| 19 | ESCAPE_ASCII = re.compile(r'([\\"]|[^\ -~])') |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 20 | HAS_UTF8 = re.compile(b'[\x80-\xff]') |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 21 | ESCAPE_DCT = { |
| 22 | '\\': '\\\\', |
| 23 | '"': '\\"', |
| 24 | '\b': '\\b', |
| 25 | '\f': '\\f', |
| 26 | '\n': '\\n', |
| 27 | '\r': '\\r', |
| 28 | '\t': '\\t', |
| 29 | } |
| 30 | for i in range(0x20): |
| 31 | ESCAPE_DCT.setdefault(chr(i), '\\u{0:04x}'.format(i)) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 32 | #ESCAPE_DCT.setdefault(chr(i), '\\u%04x' % (i,)) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 33 | |
Ezio Melotti | 898d51d | 2012-05-21 17:49:06 -0600 | [diff] [blame] | 34 | INFINITY = float('inf') |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 35 | |
Antoine Pitrou | dc3eaa8 | 2015-01-11 16:41:01 +0100 | [diff] [blame] | 36 | def py_encode_basestring(s): |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 37 | """Return a JSON representation of a Python string |
| 38 | |
| 39 | """ |
| 40 | def replace(match): |
| 41 | return ESCAPE_DCT[match.group(0)] |
| 42 | return '"' + ESCAPE.sub(replace, s) + '"' |
| 43 | |
| 44 | |
Antoine Pitrou | dc3eaa8 | 2015-01-11 16:41:01 +0100 | [diff] [blame] | 45 | encode_basestring = (c_encode_basestring or py_encode_basestring) |
| 46 | |
| 47 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 48 | def py_encode_basestring_ascii(s): |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 49 | """Return an ASCII-only JSON representation of a Python string |
| 50 | |
| 51 | """ |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 52 | def replace(match): |
| 53 | s = match.group(0) |
| 54 | try: |
| 55 | return ESCAPE_DCT[s] |
| 56 | except KeyError: |
| 57 | n = ord(s) |
| 58 | if n < 0x10000: |
| 59 | return '\\u{0:04x}'.format(n) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 60 | #return '\\u%04x' % (n,) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 61 | else: |
| 62 | # surrogate pair |
| 63 | n -= 0x10000 |
| 64 | s1 = 0xd800 | ((n >> 10) & 0x3ff) |
| 65 | s2 = 0xdc00 | (n & 0x3ff) |
| 66 | return '\\u{0:04x}\\u{1:04x}'.format(s1, s2) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 67 | return '"' + ESCAPE_ASCII.sub(replace, s) + '"' |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 68 | |
| 69 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 70 | encode_basestring_ascii = ( |
| 71 | c_encode_basestring_ascii or py_encode_basestring_ascii) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 72 | |
| 73 | class JSONEncoder(object): |
| 74 | """Extensible JSON <http://json.org> encoder for Python data structures. |
| 75 | |
| 76 | Supports the following objects and types by default: |
| 77 | |
| 78 | +-------------------+---------------+ |
| 79 | | Python | JSON | |
| 80 | +===================+===============+ |
| 81 | | dict | object | |
| 82 | +-------------------+---------------+ |
| 83 | | list, tuple | array | |
| 84 | +-------------------+---------------+ |
Georg Brandl | c8284cf | 2010-08-02 20:16:18 +0000 | [diff] [blame] | 85 | | str | string | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 86 | +-------------------+---------------+ |
Georg Brandl | c8284cf | 2010-08-02 20:16:18 +0000 | [diff] [blame] | 87 | | int, float | number | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 88 | +-------------------+---------------+ |
| 89 | | True | true | |
| 90 | +-------------------+---------------+ |
| 91 | | False | false | |
| 92 | +-------------------+---------------+ |
| 93 | | None | null | |
| 94 | +-------------------+---------------+ |
| 95 | |
| 96 | To extend this to recognize other objects, subclass and implement a |
| 97 | ``.default()`` method with another method that returns a serializable |
| 98 | object for ``o`` if possible, otherwise it should call the superclass |
| 99 | implementation (to raise ``TypeError``). |
| 100 | |
| 101 | """ |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 102 | item_separator = ', ' |
| 103 | key_separator = ': ' |
Serhiy Storchaka | aacd53f | 2016-06-22 00:03:20 +0300 | [diff] [blame] | 104 | def __init__(self, *, skipkeys=False, ensure_ascii=True, |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 105 | check_circular=True, allow_nan=True, sort_keys=False, |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 106 | indent=None, separators=None, default=None): |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 107 | """Constructor for JSONEncoder, with sensible defaults. |
| 108 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 109 | If skipkeys is false, then it is a TypeError to attempt |
Georg Brandl | c8284cf | 2010-08-02 20:16:18 +0000 | [diff] [blame] | 110 | encoding of keys that are not str, int, float or None. If |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 111 | skipkeys is True, such items are simply skipped. |
| 112 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 113 | If ensure_ascii is true, the output is guaranteed to be str |
Georg Brandl | c8284cf | 2010-08-02 20:16:18 +0000 | [diff] [blame] | 114 | objects with all incoming non-ASCII characters escaped. If |
| 115 | ensure_ascii is false, the output can contain non-ASCII characters. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 116 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 117 | If check_circular is true, then lists, dicts, and custom encoded |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 118 | objects will be checked for circular references during encoding to |
| 119 | prevent an infinite recursion (which would cause an OverflowError). |
| 120 | Otherwise, no such check takes place. |
| 121 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 122 | If allow_nan is true, then NaN, Infinity, and -Infinity will be |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 123 | encoded as such. This behavior is not JSON specification compliant, |
| 124 | but is consistent with most JavaScript based encoders and decoders. |
| 125 | Otherwise, it will be a ValueError to encode such floats. |
| 126 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 127 | If sort_keys is true, then the output of dictionaries will be |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 128 | sorted by key; this is useful for regression tests to ensure |
| 129 | that JSON serializations can be compared on a day-to-day basis. |
| 130 | |
| 131 | If indent is a non-negative integer, then JSON array |
| 132 | elements and object members will be pretty-printed with that |
| 133 | indent level. An indent level of 0 will only insert newlines. |
| 134 | None is the most compact representation. |
| 135 | |
Ezio Melotti | 1003144 | 2012-11-29 00:42:56 +0200 | [diff] [blame] | 136 | If specified, separators should be an (item_separator, key_separator) |
| 137 | tuple. The default is (', ', ': ') if *indent* is ``None`` and |
| 138 | (',', ': ') otherwise. To get the most compact JSON representation, |
| 139 | you should specify (',', ':') to eliminate whitespace. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 140 | |
| 141 | If specified, default is a function that gets called for objects |
| 142 | that can't otherwise be serialized. It should return a JSON encodable |
| 143 | version of the object or raise a ``TypeError``. |
| 144 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 145 | """ |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 146 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 147 | self.skipkeys = skipkeys |
| 148 | self.ensure_ascii = ensure_ascii |
| 149 | self.check_circular = check_circular |
| 150 | self.allow_nan = allow_nan |
| 151 | self.sort_keys = sort_keys |
| 152 | self.indent = indent |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 153 | if separators is not None: |
| 154 | self.item_separator, self.key_separator = separators |
Ezio Melotti | 1003144 | 2012-11-29 00:42:56 +0200 | [diff] [blame] | 155 | elif indent is not None: |
| 156 | self.item_separator = ',' |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 157 | if default is not None: |
| 158 | self.default = default |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 159 | |
| 160 | def default(self, o): |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 161 | """Implement this method in a subclass such that it returns |
| 162 | a serializable object for ``o``, or calls the base implementation |
| 163 | (to raise a ``TypeError``). |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 164 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 165 | For example, to support arbitrary iterators, you could |
| 166 | implement default like this:: |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 167 | |
| 168 | def default(self, o): |
| 169 | try: |
| 170 | iterable = iter(o) |
| 171 | except TypeError: |
| 172 | pass |
| 173 | else: |
| 174 | return list(iterable) |
R David Murray | dd24617 | 2013-03-17 21:52:35 -0400 | [diff] [blame] | 175 | # Let the base class default method raise the TypeError |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 176 | return JSONEncoder.default(self, o) |
| 177 | |
| 178 | """ |
Serhiy Storchaka | 47c5474 | 2016-04-10 15:46:30 +0300 | [diff] [blame] | 179 | raise TypeError("Object of type '%s' is not JSON serializable" % |
| 180 | o.__class__.__name__) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 181 | |
| 182 | def encode(self, o): |
| 183 | """Return a JSON string representation of a Python data structure. |
| 184 | |
Ethan Furman | a4998a7 | 2013-08-10 13:01:45 -0700 | [diff] [blame] | 185 | >>> from json.encoder import JSONEncoder |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 186 | >>> JSONEncoder().encode({"foo": ["bar", "baz"]}) |
| 187 | '{"foo": ["bar", "baz"]}' |
| 188 | |
| 189 | """ |
| 190 | # This is for extremely simple cases and benchmarks. |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 191 | if isinstance(o, str): |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 192 | if self.ensure_ascii: |
| 193 | return encode_basestring_ascii(o) |
| 194 | else: |
| 195 | return encode_basestring(o) |
| 196 | # This doesn't pass the iterator directly to ''.join() because the |
| 197 | # exceptions aren't as detailed. The list call should be roughly |
| 198 | # equivalent to the PySequence_Fast that ''.join() would do. |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 199 | chunks = self.iterencode(o, _one_shot=True) |
| 200 | if not isinstance(chunks, (list, tuple)): |
| 201 | chunks = list(chunks) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 202 | return ''.join(chunks) |
| 203 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 204 | def iterencode(self, o, _one_shot=False): |
| 205 | """Encode the given object and yield each string |
| 206 | representation as available. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 207 | |
| 208 | For example:: |
| 209 | |
| 210 | for chunk in JSONEncoder().iterencode(bigobject): |
| 211 | mysocket.write(chunk) |
| 212 | |
| 213 | """ |
| 214 | if self.check_circular: |
| 215 | markers = {} |
| 216 | else: |
| 217 | markers = None |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 218 | if self.ensure_ascii: |
| 219 | _encoder = encode_basestring_ascii |
| 220 | else: |
| 221 | _encoder = encode_basestring |
| 222 | |
| 223 | def floatstr(o, allow_nan=self.allow_nan, |
Serhiy Storchaka | e0805cf | 2016-04-10 14:41:19 +0300 | [diff] [blame] | 224 | _repr=float.__repr__, _inf=INFINITY, _neginf=-INFINITY): |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 225 | # Check for specials. Note that this type of test is processor |
| 226 | # and/or platform-specific, so do tests which don't depend on the |
| 227 | # internals. |
| 228 | |
| 229 | if o != o: |
| 230 | text = 'NaN' |
| 231 | elif o == _inf: |
| 232 | text = 'Infinity' |
| 233 | elif o == _neginf: |
| 234 | text = '-Infinity' |
| 235 | else: |
| 236 | return _repr(o) |
| 237 | |
| 238 | if not allow_nan: |
| 239 | raise ValueError( |
| 240 | "Out of range float values are not JSON compliant: " + |
| 241 | repr(o)) |
| 242 | |
| 243 | return text |
| 244 | |
| 245 | |
| 246 | if (_one_shot and c_make_encoder is not None |
R David Murray | 3dd02d6 | 2011-04-12 21:02:45 -0400 | [diff] [blame] | 247 | and self.indent is None): |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 248 | _iterencode = c_make_encoder( |
| 249 | markers, self.default, _encoder, self.indent, |
| 250 | self.key_separator, self.item_separator, self.sort_keys, |
| 251 | self.skipkeys, self.allow_nan) |
| 252 | else: |
| 253 | _iterencode = _make_iterencode( |
| 254 | markers, self.default, _encoder, self.indent, floatstr, |
| 255 | self.key_separator, self.item_separator, self.sort_keys, |
| 256 | self.skipkeys, _one_shot) |
| 257 | return _iterencode(o, 0) |
| 258 | |
| 259 | def _make_iterencode(markers, _default, _encoder, _indent, _floatstr, |
| 260 | _key_separator, _item_separator, _sort_keys, _skipkeys, _one_shot, |
| 261 | ## HACK: hand-optimized bytecode; turn globals into locals |
| 262 | ValueError=ValueError, |
| 263 | dict=dict, |
| 264 | float=float, |
| 265 | id=id, |
| 266 | int=int, |
| 267 | isinstance=isinstance, |
| 268 | list=list, |
| 269 | str=str, |
| 270 | tuple=tuple, |
Serhiy Storchaka | e0805cf | 2016-04-10 14:41:19 +0300 | [diff] [blame] | 271 | _intstr=int.__str__, |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 272 | ): |
| 273 | |
Raymond Hettinger | b643ef8 | 2010-10-31 08:00:16 +0000 | [diff] [blame] | 274 | if _indent is not None and not isinstance(_indent, str): |
| 275 | _indent = ' ' * _indent |
| 276 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 277 | def _iterencode_list(lst, _current_indent_level): |
| 278 | if not lst: |
| 279 | yield '[]' |
| 280 | return |
| 281 | if markers is not None: |
| 282 | markerid = id(lst) |
| 283 | if markerid in markers: |
| 284 | raise ValueError("Circular reference detected") |
| 285 | markers[markerid] = lst |
| 286 | buf = '[' |
| 287 | if _indent is not None: |
| 288 | _current_indent_level += 1 |
Raymond Hettinger | b643ef8 | 2010-10-31 08:00:16 +0000 | [diff] [blame] | 289 | newline_indent = '\n' + _indent * _current_indent_level |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 290 | separator = _item_separator + newline_indent |
| 291 | buf += newline_indent |
| 292 | else: |
| 293 | newline_indent = None |
| 294 | separator = _item_separator |
| 295 | first = True |
| 296 | for value in lst: |
| 297 | if first: |
| 298 | first = False |
| 299 | else: |
| 300 | buf = separator |
| 301 | if isinstance(value, str): |
| 302 | yield buf + _encoder(value) |
| 303 | elif value is None: |
| 304 | yield buf + 'null' |
| 305 | elif value is True: |
| 306 | yield buf + 'true' |
| 307 | elif value is False: |
| 308 | yield buf + 'false' |
| 309 | elif isinstance(value, int): |
Ethan Furman | a4998a7 | 2013-08-10 13:01:45 -0700 | [diff] [blame] | 310 | # Subclasses of int/float may override __str__, but we still |
| 311 | # want to encode them as integers/floats in JSON. One example |
| 312 | # within the standard library is IntEnum. |
Serhiy Storchaka | e0805cf | 2016-04-10 14:41:19 +0300 | [diff] [blame] | 313 | yield buf + _intstr(value) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 314 | elif isinstance(value, float): |
Ethan Furman | a4998a7 | 2013-08-10 13:01:45 -0700 | [diff] [blame] | 315 | # see comment above for int |
Serhiy Storchaka | e0805cf | 2016-04-10 14:41:19 +0300 | [diff] [blame] | 316 | yield buf + _floatstr(value) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 317 | else: |
| 318 | yield buf |
| 319 | if isinstance(value, (list, tuple)): |
| 320 | chunks = _iterencode_list(value, _current_indent_level) |
| 321 | elif isinstance(value, dict): |
| 322 | chunks = _iterencode_dict(value, _current_indent_level) |
| 323 | else: |
| 324 | chunks = _iterencode(value, _current_indent_level) |
Philip Jenvey | fd0d3e5 | 2012-10-01 15:34:31 -0700 | [diff] [blame] | 325 | yield from chunks |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 326 | if newline_indent is not None: |
| 327 | _current_indent_level -= 1 |
Raymond Hettinger | b643ef8 | 2010-10-31 08:00:16 +0000 | [diff] [blame] | 328 | yield '\n' + _indent * _current_indent_level |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 329 | yield ']' |
| 330 | if markers is not None: |
| 331 | del markers[markerid] |
| 332 | |
| 333 | def _iterencode_dict(dct, _current_indent_level): |
| 334 | if not dct: |
| 335 | yield '{}' |
| 336 | return |
| 337 | if markers is not None: |
| 338 | markerid = id(dct) |
| 339 | if markerid in markers: |
| 340 | raise ValueError("Circular reference detected") |
| 341 | markers[markerid] = dct |
| 342 | yield '{' |
| 343 | if _indent is not None: |
| 344 | _current_indent_level += 1 |
Raymond Hettinger | b643ef8 | 2010-10-31 08:00:16 +0000 | [diff] [blame] | 345 | newline_indent = '\n' + _indent * _current_indent_level |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 346 | item_separator = _item_separator + newline_indent |
| 347 | yield newline_indent |
| 348 | else: |
| 349 | newline_indent = None |
| 350 | item_separator = _item_separator |
| 351 | first = True |
| 352 | if _sort_keys: |
| 353 | items = sorted(dct.items(), key=lambda kv: kv[0]) |
| 354 | else: |
| 355 | items = dct.items() |
| 356 | for key, value in items: |
| 357 | if isinstance(key, str): |
| 358 | pass |
| 359 | # JavaScript is weakly typed for these, so it makes sense to |
| 360 | # also allow them. Many encoders seem to do something like this. |
| 361 | elif isinstance(key, float): |
Ethan Furman | a4998a7 | 2013-08-10 13:01:45 -0700 | [diff] [blame] | 362 | # see comment for int/float in _make_iterencode |
Serhiy Storchaka | e0805cf | 2016-04-10 14:41:19 +0300 | [diff] [blame] | 363 | key = _floatstr(key) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 364 | elif key is True: |
| 365 | key = 'true' |
| 366 | elif key is False: |
| 367 | key = 'false' |
| 368 | elif key is None: |
| 369 | key = 'null' |
| 370 | elif isinstance(key, int): |
Ethan Furman | a4998a7 | 2013-08-10 13:01:45 -0700 | [diff] [blame] | 371 | # see comment for int/float in _make_iterencode |
Serhiy Storchaka | e0805cf | 2016-04-10 14:41:19 +0300 | [diff] [blame] | 372 | key = _intstr(key) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 373 | elif _skipkeys: |
| 374 | continue |
| 375 | else: |
| 376 | raise TypeError("key " + repr(key) + " is not a string") |
| 377 | if first: |
| 378 | first = False |
| 379 | else: |
| 380 | yield item_separator |
| 381 | yield _encoder(key) |
| 382 | yield _key_separator |
| 383 | if isinstance(value, str): |
| 384 | yield _encoder(value) |
| 385 | elif value is None: |
| 386 | yield 'null' |
| 387 | elif value is True: |
| 388 | yield 'true' |
| 389 | elif value is False: |
| 390 | yield 'false' |
| 391 | elif isinstance(value, int): |
Ethan Furman | a4998a7 | 2013-08-10 13:01:45 -0700 | [diff] [blame] | 392 | # see comment for int/float in _make_iterencode |
Serhiy Storchaka | e0805cf | 2016-04-10 14:41:19 +0300 | [diff] [blame] | 393 | yield _intstr(value) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 394 | elif isinstance(value, float): |
Ethan Furman | a4998a7 | 2013-08-10 13:01:45 -0700 | [diff] [blame] | 395 | # see comment for int/float in _make_iterencode |
Serhiy Storchaka | e0805cf | 2016-04-10 14:41:19 +0300 | [diff] [blame] | 396 | yield _floatstr(value) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 397 | else: |
| 398 | if isinstance(value, (list, tuple)): |
| 399 | chunks = _iterencode_list(value, _current_indent_level) |
| 400 | elif isinstance(value, dict): |
| 401 | chunks = _iterencode_dict(value, _current_indent_level) |
| 402 | else: |
| 403 | chunks = _iterencode(value, _current_indent_level) |
Philip Jenvey | fd0d3e5 | 2012-10-01 15:34:31 -0700 | [diff] [blame] | 404 | yield from chunks |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 405 | if newline_indent is not None: |
| 406 | _current_indent_level -= 1 |
Raymond Hettinger | b643ef8 | 2010-10-31 08:00:16 +0000 | [diff] [blame] | 407 | yield '\n' + _indent * _current_indent_level |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 408 | yield '}' |
| 409 | if markers is not None: |
| 410 | del markers[markerid] |
| 411 | |
| 412 | def _iterencode(o, _current_indent_level): |
| 413 | if isinstance(o, str): |
| 414 | yield _encoder(o) |
| 415 | elif o is None: |
| 416 | yield 'null' |
| 417 | elif o is True: |
| 418 | yield 'true' |
| 419 | elif o is False: |
| 420 | yield 'false' |
Florent Xicluna | 02ea12b2 | 2010-07-28 16:39:41 +0000 | [diff] [blame] | 421 | elif isinstance(o, int): |
Ethan Furman | a4998a7 | 2013-08-10 13:01:45 -0700 | [diff] [blame] | 422 | # see comment for int/float in _make_iterencode |
Serhiy Storchaka | e0805cf | 2016-04-10 14:41:19 +0300 | [diff] [blame] | 423 | yield _intstr(o) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 424 | elif isinstance(o, float): |
Ethan Furman | a4998a7 | 2013-08-10 13:01:45 -0700 | [diff] [blame] | 425 | # see comment for int/float in _make_iterencode |
Serhiy Storchaka | e0805cf | 2016-04-10 14:41:19 +0300 | [diff] [blame] | 426 | yield _floatstr(o) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 427 | elif isinstance(o, (list, tuple)): |
Philip Jenvey | fd0d3e5 | 2012-10-01 15:34:31 -0700 | [diff] [blame] | 428 | yield from _iterencode_list(o, _current_indent_level) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 429 | elif isinstance(o, dict): |
Philip Jenvey | fd0d3e5 | 2012-10-01 15:34:31 -0700 | [diff] [blame] | 430 | yield from _iterencode_dict(o, _current_indent_level) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 431 | else: |
| 432 | if markers is not None: |
| 433 | markerid = id(o) |
| 434 | if markerid in markers: |
| 435 | raise ValueError("Circular reference detected") |
| 436 | markers[markerid] = o |
| 437 | o = _default(o) |
Philip Jenvey | fd0d3e5 | 2012-10-01 15:34:31 -0700 | [diff] [blame] | 438 | yield from _iterencode(o, _current_indent_level) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 439 | if markers is not None: |
| 440 | del markers[markerid] |
| 441 | return _iterencode |