Was trying to open an issue on GigHub but apparently the POST access is blocked from the location I was trying to do it, so here is a simple code representing the issue:
Consider the code below. The object of the NonIteratorClass will be represented correctly while IteratorClass's representation will miss any internal details. Moreover an instance of InfiniteIteratorClass will never be encoded (MemoryError after a very long delay):
import jsonpickle
class Element( object ):
"""Iterator element"""
def __init__( self, i ):
self.i = i
class NonIteratorClass( object ):
"""Plain class with __dict__"""
def __init__( self ):
self.my_data = [ 1,2,3,4,5 ]
def print_my_data( self ):
print self.my_data
class IteratorClass( NonIteratorClass ):
"""Class with __dict__ implementing basic Iterator protocol
and producing a limited number of elements
"""
def __init__( self ):
super( IteratorClass, self ).__init__()
self.current = -1
def __len__( self ):
return len( self.my_data )
def __iter__( self ):
return self
def next( self ):
try:
self.current += 1
return Element( self.my_data[ self.current ] )
except IndexError:
raise StopIteration
class InfiniteIteratorClass( IteratorClass ):
"""Class with __dict__ implementing basic Iterator protocol
and producing infinite stream of elements
"""
def __init__( self ):
super( InfiniteIteratorClass, self ).__init__()
def next( self ):
return Element( 1 )
if __name__ == "__main__":
nio = NonIteratorClass()
io = IteratorClass()
iio = InfiniteIteratorClass()
print "Encoding NonIteratorClass instance:"
print jsonpickle.encode( nio )
print
print "Encoding IteratorClass instance ( where is io.my_data ??? ):"
print jsonpickle.encode( io )
print
print "Encoding InfiniteIteratorClass instance ( jsonpickle.encode() never returns with MemoryError eventually ):"
print jsonpickle.encode( iio )
The output of the code is provided below:
Encoding NonIteratorClass instance:
{"py/object": "__main__.NonIteratorClass", "my_data": [1, 2, 3, 4, 5]}
Encoding IteratorClass instance ( where is io.my_data ??? ):
{"py/object": "__main__.IteratorClass", "py/iterator": [{"py/object": "__main__.Element", "i": 1}, {"py/object": "__main__.Element", "i": 2}, {"py/object": "__main__.Element", "i": 3}, {"py/object": "__main__.Element", "i": 4}, {"py/object": "__main__.Element", "i": 5}]}
Encoding InfiniteIteratorClass instance ( jsonpickle.encode() never returns with MemoryError eventually ):