Encode the given object and yield each string representation as available. For example:: for chunk in JSONEncoder().iterencode(bigobject): mysocket.write(chunk)
(self, o, _one_shot=False)
| 232 | return u''.join(chunks) |
| 233 | |
| 234 | def iterencode(self, o, _one_shot=False): |
| 235 | """Encode the given object and yield each string |
| 236 | representation as available. |
| 237 | |
| 238 | For example:: |
| 239 | |
| 240 | for chunk in JSONEncoder().iterencode(bigobject): |
| 241 | mysocket.write(chunk) |
| 242 | |
| 243 | """ |
| 244 | if self.check_circular: |
| 245 | markers = {} |
| 246 | else: |
| 247 | markers = None |
| 248 | if self.ensure_ascii: |
| 249 | _encoder = encode_basestring_ascii |
| 250 | else: |
| 251 | _encoder = encode_basestring |
| 252 | if self.encoding != 'utf-8': |
| 253 | def _encoder(o, _orig_encoder=_encoder, _encoding=self.encoding): |
| 254 | if isinstance(o, str): |
| 255 | o = o.decode(_encoding) |
| 256 | return _orig_encoder(o) |
| 257 | |
| 258 | def floatstr(o, allow_nan=self.allow_nan, |
| 259 | _repr=FLOAT_REPR, _inf=PosInf, _neginf=-PosInf): |
| 260 | # Check for specials. Note that this type of test is processor |
| 261 | # and/or platform-specific, so do tests which don't depend on |
| 262 | # the internals. |
| 263 | |
| 264 | if o != o: |
| 265 | text = 'NaN' |
| 266 | elif o == _inf: |
| 267 | text = 'Infinity' |
| 268 | elif o == _neginf: |
| 269 | text = '-Infinity' |
| 270 | else: |
| 271 | return _repr(o) |
| 272 | |
| 273 | if not allow_nan: |
| 274 | raise ValueError( |
| 275 | "Out of range float values are not JSON compliant: " + |
| 276 | repr(o)) |
| 277 | |
| 278 | return text |
| 279 | |
| 280 | |
| 281 | key_memo = {} |
| 282 | if (_one_shot and c_make_encoder is not None |
| 283 | and self.indent is None): |
| 284 | _iterencode = c_make_encoder( |
| 285 | markers, self.default, _encoder, self.indent, |
| 286 | self.key_separator, self.item_separator, self.sort_keys, |
| 287 | self.skipkeys, self.allow_nan, key_memo, self.use_decimal, |
| 288 | self.namedtuple_as_object, self.tuple_as_array) |
| 289 | else: |
| 290 | _iterencode = _make_iterencode( |
| 291 | markers, self.default, _encoder, self.indent, floatstr, |
no test coverage detected