PyDocs

Nav

Internet Data Handling

JSONEncoder.iterencode

json

method of JSONEncoder (source)
  • JSONEncoder.iterencode(o)
Encode the given object and yield each string
representation as available.

For example::

    for chunk in JSONEncoder().iterencode(bigobject):
        mysocket.write(chunk)
1 Example
>>> import json
>>> class ComplexEncoder(json.JSONEncoder):
...     def default(self, obj):
...         if isinstance(obj, complex):
...             return [obj.real, obj.imag]
...         # Let the base class default method raise the TypeError
...         return super().default(obj)
...
>>> json.dumps(2 + 1j, cls=ComplexEncoder)
'[2.0, 1.0]'
>>> ComplexEncoder().encode(2 + 1j)
'[2.0, 1.0]'
>>> list(ComplexEncoder().iterencode(2 + 1j))
['[2.0', ', 1.0', ']']
from the Python docs
See Also
No see-alsos for json/JSONEncoder.iterencode
0 Notes
No notes for JSONEncoder.iterencode