Encode the given object and yield each string representation as available. For example:: for chunk in JSONEncoder().iterencode(bigobject): mysocket.write(chunk)
(self, o)
| 319 | return u''.join(chunks) |
| 320 | |
| 321 | def iterencode(self, o): |
| 322 | """Encode the given object and yield each string |
| 323 | representation as available. |
| 324 | |
| 325 | For example:: |
| 326 | |
| 327 | for chunk in JSONEncoder().iterencode(bigobject): |
| 328 | mysocket.write(chunk) |
| 329 | |
| 330 | """ |
| 331 | if self.check_circular: |
| 332 | markers = {} |
| 333 | else: |
| 334 | markers = None |
| 335 | if self.ensure_ascii: |
| 336 | _encoder = encode_basestring_ascii |
| 337 | else: |
| 338 | _encoder = encode_basestring |
| 339 | if self.encoding != 'utf-8' and self.encoding is not None: |
| 340 | def _encoder(o, _orig_encoder=_encoder, _encoding=self.encoding): |
| 341 | if isinstance(o, binary_type): |
| 342 | o = text_type(o, _encoding) |
| 343 | return _orig_encoder(o) |
| 344 | |
| 345 | def floatstr(o, allow_nan=self.allow_nan, ignore_nan=self.ignore_nan, |
| 346 | _repr=FLOAT_REPR, _inf=PosInf, _neginf=-PosInf): |
| 347 | # Check for specials. Note that this type of test is processor |
| 348 | # and/or platform-specific, so do tests which don't depend on |
| 349 | # the internals. |
| 350 | |
| 351 | if o != o: |
| 352 | text = 'NaN' |
| 353 | elif o == _inf: |
| 354 | text = 'Infinity' |
| 355 | elif o == _neginf: |
| 356 | text = '-Infinity' |
| 357 | else: |
| 358 | if type(o) != float: |
| 359 | # See #118, do not trust custom str/repr |
| 360 | o = float(o) |
| 361 | return _repr(o) |
| 362 | |
| 363 | if ignore_nan: |
| 364 | text = 'null' |
| 365 | elif not allow_nan: |
| 366 | raise ValueError( |
| 367 | "Out of range float values are not JSON compliant: " + |
| 368 | repr(o)) |
| 369 | |
| 370 | return text |
| 371 | |
| 372 | key_memo = {} |
| 373 | int_as_string_bitcount = ( |
| 374 | 53 if self.bigint_as_string else self.int_as_string_bitcount) |
| 375 | if c_make_encoder is not None: |
| 376 | _iterencode = c_make_encoder( |
| 377 | markers, self.default, _encoder, self.indent, |
| 378 | self.key_separator, self.item_separator, self.sort_keys, |