【发布时间】:2019-07-26 09:58:51
【问题描述】:
当我使用 jsonpickle 对自定义的可迭代类进行编码和解码时,包含的项目会加倍。
我尝试使用 demjson 和 simplejson 并尝试实现这个https://docs.python.org/2.5/ref/sequence-types.html。 如果我从列表继承它确实有效。但我不想继承。 如果我不实施 iter
也可以我有这样的课:
import jsonpickle
from typing import *
class Product:
def __init__(self, name):
self.name = name
class Products:
def __init__(self):
self.__products: List[Product] = list()
def append(self, product: Product):
self.__products.append(product)
def __iter__(self):
return iter(self.__products)
def __next__(self):
return next(self.__products)
def __len__(self):
return len(self.__products)
def __getitem__(self, i):
return self.__products[i]
def extend(self, products: Iterable[Product]):
self.__products.extend(products)
当我使用 jsonpickle 对此类进行编码并再次对其进行解码时,包含的产品会翻倍。此示例中引发了 ValueError
if __name__ == '__main__':
products = Products()
products.append(Product('abc'))
encoded = jsonpickle.encode(products)
decoded_products = jsonpickle.decode(encoded)
if len(decoded_products) == 2:
raise ValueError()
如果我使用encoded = jsonpickle.encode(products, make_refs=False),第二个对象是字符串而不是产品
我是否必须实施任何其他方法才能使其正常工作?
【问题讨论】:
标签: python iterable jsonpickle