1

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

1 に答える 1

2

私はこの同じ問題に遭遇し、配列の宣言を init 内に移動することで解決できました。

class Collection(object):
    label = ""
    # secrets = [] (move this into __init__)

   def __init__(self, collection):
        self.secrets = []
        self.label = collection.get_label()
        for secret in collection.get_all_items():
            self.secrets.append(Secret(secret))
于 2016-03-06T19:22:59.707 に答える