Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fails to encode attribute of nested collections.defaultdict #75

Closed
kylebgorman opened this issue Jul 7, 2014 · 1 comment
Closed

Fails to encode attribute of nested collections.defaultdict #75

kylebgorman opened this issue Jul 7, 2014 · 1 comment

Comments

@kylebgorman
Copy link

I use nested defaultdicts to represent sparse matrices. With default settings (i.e., no directives to the backend) on Python 2.7.5 and Python 3.4.1, the outer default_factory is simply not encoded. The following example shows this:

import jsonpickle
import collections
mydict = collections.defaultdict(lambda: collections.defaultdict(int))
mydict[3][2] = 4
encoded = jsonpickle.encode(mydict)
print(encoded)

This prints:

{"py/object": "collections.defaultdict", "3": {"default_factory": {"py/type": "__builtin__.int"}, "2": 4, "py/object": "collections.defaultdict"}}

This is wrong: there's no outer default_factory at all. (Note this has nothing to do with the use of a lambda function, as far as I can tell: non-anonymous default_factories behave the same way.)

This also doesn't go round-trip:

mydict_new = jsonpickle.decode(encoded)
print(mydict.default_factory) # prints pointer to the lambda function
print(mydict_new.default_factory) # prints None
davvid added a commit that referenced this issue Jul 10, 2014
Related-to: #75
Signed-off-by: David Aguilar <davvid@gmail.com>
@davvid davvid closed this as completed in 1f0d7a3 Jul 10, 2014
@davvid
Copy link
Member

davvid commented Jul 10, 2014

You're using integer keys so make sure to pass keys=True when calling encode and decode as demonstrated in the unit test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants