Better support for cyclical data structures #16

Closed
wants to merge 6 commits into
from

Conversation

Projects
None yet
2 participants
@davvid
Member

davvid commented Jun 19, 2011

This fixes issue #12 by improving the method we use for creating object references.

I think you'll find that the new method is both simpler and more robust. I kept backwards compatibility (for reading) so there should be little harm when someone upgrades to a newer jsonpickle.

I also tweaked the test suite so that it skips some backend tests if the backends are not available. Let me know if you have any questions.

I'll start a thread on the mailing list describing the new algorithm so that others are aware of it.

davvid and others added some commits Jun 18, 2011

tests: Skip demjson, yajl, and jsonlib tests when not available
Contributors might not always have all backends installed
so skip the backend tests when the modules are not installed.

Signed-off-by: David Aguilar <davvid@gmail.com>
tests: Add a document data structure to the sample classes
Signed-off-by: David Aguilar <davvid@gmail.com>
Better support for cyclical data structures
One limitation of the initial "referenced object ID" implementation was
that it relied on a special "xquery"-like "/foo/bar/baz" object name
notation.  This was a simple mapping that could only map into objects
and their attributes; it fell apart when the object it needed to
reference first appeared as an element of a list.

jsonpickle now uses an ID-based object reference notation.
The pickler keeps track of objects it has seen and assigns each
a unique number as it is traversed.  That object's JSON representation
is represented by, e.g. {"py/id": 42} later when that same object is
seen elsewhere in an object graph.

This method is robust enough to index into list elements because the
lookup is id-based instead of an ad-hoc query format.

The pickler no longer writes the old {"py/ref": "/foo/bar"} reference
dicts, it only writes {"py/id": int} references.

The unpickler, though, understands both notations and can still unpickle
JSON created by earlier versions of jsonpickle.  This keeps backwards
compatibility so that we can still read files written by earlier versions
of the pickler.

Closes #12

Signed-off-by: David Aguilar <davvid@gmail.com>
tests: Remove spurious print statement
Signed-off-by: David Aguilar <davvid@gmail.com>
tests: Add stricter checking in test_list_of_objects()
Signed-off-by: David Aguilar <davvid@gmail.com>
@johnpaulett

This comment has been minimized.

Show comment
Hide comment
@johnpaulett

johnpaulett Jun 21, 2011

Contributor

Looks great!

Contributor

johnpaulett commented Jun 21, 2011

Looks great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment