我想序列化一个包含嵌套列表的 python 列表。下面的代码构造了要从 gnome 密钥环序列化的对象,但jsonpickle编码器不会序列化子列表。有了unpicklable=True
,我简单地得到:
[{"py/object": "__main__.Collection", "label": ""}, {"py/object": "__main__.Collection", "label": "Login"}]
我已经尝试过设置/不设置max_depth
并尝试了很多深度数字,但无论如何,腌制器只会腌制顶级物品。
如何让它序列化整个对象结构?
#! /usr/bin/env python
import secretstorage
import jsonpickle
class Secret(object):
label = ""
username = ""
password = ""
def __init__(self, secret):
self.label = secret.get_label()
self.password = '%s' % secret.get_secret()
attributes = secret.get_attributes()
if attributes and 'username_value' in attributes:
self.username = '%s' % attributes['username_value']
class Collection(object):
label = ""
secrets = []
def __init__(self, collection):
self.label = collection.get_label()
for secret in collection.get_all_items():
self.secrets.append(Secret(secret))
def keyring_to_json():
collections = []
bus = secretstorage.dbus_init()
for collection in secretstorage.get_all_collections(bus):
collections.append(Collection(collection))
pickle = jsonpickle.encode(collections, unpicklable=False);
print(pickle)
if __name__ == '__main__':
keyring_to_json()