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)
| 205 | return ''.join(chunks) |
| 206 | |
| 207 | def iterencode(self, o, _one_shot=False): |
| 208 | """Encode the given object and yield each string |
| 209 | representation as available. |
| 210 | |
| 211 | For example:: |
| 212 | |
| 213 | for chunk in JSONEncoder().iterencode(bigobject): |
| 214 | mysocket.write(chunk) |
| 215 | |
| 216 | """ |
| 217 | if self.check_circular: |
| 218 | markers = {} |
| 219 | else: |
| 220 | markers = None |
| 221 | if self.ensure_ascii: |
| 222 | _encoder = encode_basestring_ascii |
| 223 | else: |
| 224 | _encoder = encode_basestring |
| 225 | |
| 226 | def floatstr(o, allow_nan=self.allow_nan, |
| 227 | _repr=float.__repr__, _inf=INFINITY, _neginf=-INFINITY): |
| 228 | # Check for specials. Note that this type of test is processor |
| 229 | # and/or platform-specific, so do tests which don't depend on the |
| 230 | # internals. |
| 231 | |
| 232 | if o != o: |
| 233 | text = 'NaN' |
| 234 | elif o == _inf: |
| 235 | text = 'Infinity' |
| 236 | elif o == _neginf: |
| 237 | text = '-Infinity' |
| 238 | else: |
| 239 | return _repr(o) |
| 240 | |
| 241 | if not allow_nan: |
| 242 | raise ValueError( |
| 243 | "Out of range float values are not JSON compliant: " + |
| 244 | repr(o)) |
| 245 | |
| 246 | return text |
| 247 | |
| 248 | |
| 249 | if self.indent is None or isinstance(self.indent, str): |
| 250 | indent = self.indent |
| 251 | else: |
| 252 | indent = ' ' * self.indent |
| 253 | if _one_shot and c_make_encoder is not None: |
| 254 | _iterencode = c_make_encoder( |
| 255 | markers, self.default, _encoder, indent, |
| 256 | self.key_separator, self.item_separator, self.sort_keys, |
| 257 | self.skipkeys, self.allow_nan) |
| 258 | else: |
| 259 | _iterencode = _make_iterencode( |
| 260 | markers, self.default, _encoder, indent, floatstr, |
| 261 | self.key_separator, self.item_separator, self.sort_keys, |
| 262 | self.skipkeys, _one_shot) |
| 263 | return _iterencode(o, 0) |
| 264 |