| 263 | return _iterencode(o, 0) |
| 264 | |
| 265 | def _make_iterencode(markers, _default, _encoder, _indent, _floatstr, |
| 266 | _key_separator, _item_separator, _sort_keys, _skipkeys, _one_shot, |
| 267 | ## HACK: hand-optimized bytecode; turn globals into locals |
| 268 | ValueError=ValueError, |
| 269 | dict=dict, |
| 270 | float=float, |
| 271 | id=id, |
| 272 | int=int, |
| 273 | isinstance=isinstance, |
| 274 | list=list, |
| 275 | str=str, |
| 276 | tuple=tuple, |
| 277 | _intstr=int.__repr__, |
| 278 | ): |
| 279 | |
| 280 | def _iterencode_list(lst, _current_indent_level): |
| 281 | if not lst: |
| 282 | yield '[]' |
| 283 | return |
| 284 | if markers is not None: |
| 285 | markerid = id(lst) |
| 286 | if markerid in markers: |
| 287 | raise ValueError("Circular reference detected") |
| 288 | markers[markerid] = lst |
| 289 | buf = '[' |
| 290 | if _indent is not None: |
| 291 | _current_indent_level += 1 |
| 292 | newline_indent = '\n' + _indent * _current_indent_level |
| 293 | separator = _item_separator + newline_indent |
| 294 | buf += newline_indent |
| 295 | else: |
| 296 | newline_indent = None |
| 297 | separator = _item_separator |
| 298 | for i, value in enumerate(lst): |
| 299 | if i: |
| 300 | buf = separator |
| 301 | try: |
| 302 | if isinstance(value, str): |
| 303 | yield buf + _encoder(value) |
| 304 | elif value is None: |
| 305 | yield buf + 'null' |
| 306 | elif value is True: |
| 307 | yield buf + 'true' |
| 308 | elif value is False: |
| 309 | yield buf + 'false' |
| 310 | elif isinstance(value, int): |
| 311 | # Subclasses of int/float may override __repr__, but we still |
| 312 | # want to encode them as integers/floats in JSON. One example |
| 313 | # within the standard library is IntEnum. |
| 314 | yield buf + _intstr(value) |
| 315 | elif isinstance(value, float): |
| 316 | # see comment above for int |
| 317 | yield buf + _floatstr(value) |
| 318 | else: |
| 319 | yield buf |
| 320 | if isinstance(value, (list, tuple)): |
| 321 | chunks = _iterencode_list(value, _current_indent_level) |
| 322 | elif isinstance(value, dict): |