ネストされたリストを含む 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()