I'd like to serialise a python list that contains nested lists. The code below constructs the object to be serialised from a gnome keyring but the jsonpickle encoder, doesn't serialise the child lists. With unpicklable=True
, I simply get:
[{"py/object": "__main__.Collection", "label": ""}, {"py/object": "__main__.Collection", "label": "Login"}]
I've experimented with setting/not setting max_depth
and tried lots of depth numbers, but regardless, the pickler will only pickle the top level items.
How do I make it serialise the entire object structure?
#! /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()
I ran into this same problem, and was able to resolve it by moving the declaration of arrays inside of the 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))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.