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

Better support for cyclical data structures #16

wants to merge 6 commits into
base: master


None yet
2 participants
Copy link

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 <>
tests: Add a document data structure to the sample classes
Signed-off-by: David Aguilar <>
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 <>
tests: Remove spurious print statement
Signed-off-by: David Aguilar <>
tests: Add stricter checking in test_list_of_objects()
Signed-off-by: David Aguilar <>

This comment has been minimized.

Copy link

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