_mkref format & limitation

2 views
Skip to first unread message

Eoghan Murray

unread,
Nov 22, 2009, 2:30:23 PM11/22/09
to jsonpickle, dav...@gmail.com
_mkref / _refname can't handle objects in lists yet:

def test_list_of_objects(self):
"""Test that objects in lists are referenced correctly"""
a = Thing('a')
b = Thing('b')
pickled = jsonpickle.encode([a, b, b])
unpickled = jsonpickle.decode(pickled)
self.assertEqual(unpickled[1], unpickled[2])

what is returned is [a,b,a] instead of [a, b, b]

Maybe py/ref needs to be extended with a bit of list indexing, e.g. in
>>> jsonpickle.encode({'l1': {'l2': [a, b, b]}})
have the second 'b' encoded as
{"py/ref": "/l1/l2[1]"} rather than {"py/ref": "/l1/l2"}]}}'

Cheers,

Eoghan

Eoghan Murray

unread,
Nov 22, 2009, 3:30:04 PM11/22/09
to jsonpickle

Eoghan Murray

unread,
Nov 22, 2009, 5:20:22 PM11/22/09
to jsonpickle
Ok now I think I understand the following:

2009/10/24 David Aguilar <dav[..]@gmail.com>
> The referencing code needs a rework too. We want to be able to
> reconstruct refs into array elements, and presumably it'd be
> better if we were able to do so for any arbitrary objects as
> keys, not just dicts, but dicts are a good starting point.

[snip]
>
> Once it's well tested then the
> work of getting it to support refs like the following would be
> something to think about:
>
> class A(object):
> pass
>
> a = A()
> a.items = [A(), A(), A()]
> a.first = a.items[0]
> a.stuff = {a.first: a.first}
> a.morestuff = {a.stuff: a.stuff}
>
> pickle = jsonpickle.encode(a)
> b = jsonpickle.decode(pickle)
>
> item = b.items[0]
> self.assertEqual(b.first, item)
> self.assertEqual(b.stuff[b.first], item)
> self.assertEqual(b.morestuff[b.stuff][b.first], item)


I've created a branch which adds the above tests for complex
referencing:

http://github.com/eoghanmurray/jsonpickle/tree/refs

I'm awaiting an answer to the following:

http://code.google.com/p/jsonpath/issues/detail?id=14

(which would address test_refs_keys_values)

to see if jsonpath is a good candidate to solve all the referencing
problems.

E
Reply all
Reply to author
Forward
0 new messages