Fork me on GitHub

genosha by unayok

Python object graph serialization/deserialization over JSON or XML.
GENOSHA (GENeral Object marSHAller) is a library to allow serialization of Python object graphs. While pickle performs this task, pickles are not always appropriate, for example, if a serialized object might need to be manipulated by an outside tool (in the simplest example: a text editor or shell script).

By itself, the genosha module provides means to represent a set of objects (an object graph) in a linearly-serializable manner. Objects of all types (aside from “primitives” such as ints and bools) are abstracted into GenoshaObjects; references to other objects (represented as other GenoshaObjects are abstracted into GenoshaReferences. Like :mod:`pickle` there are limitations to what it is capable of. The good news is that Genosha handles a few more situations than pickle. The limitations are still of a similar kind, however. The following types of objects cannot be marshalled using Genosha (not comprehensive, but illustrative):

GenoshaObject? and GenoshaReference? contain the information necessary to reconstruct the original objects (including references and cycles of references as necessary).

The creation of the serialization structures is performed in memory; the output is not streamable.

There are two serialization modules provided. genosha.JSON provides JSON serialization/deserialization. genosha.XML provides and XML implementation using ElementTree. Each of the modules provides an interface that users of pickle should find familiar (dump, dumps, load, loads).

Usage of the genosha module is relatively straightforward.

>>> out = genosha.marshal( obj )
>>> obj_again = genosha.unmarshal( out )

It is probably a good idea to catch TypeErrors, as these are thrown when genosha encounters a situation it can’t handle (e.g. one of the above-listed problematic situations).

Similarly, it is likely you’ll want to use either the genosha.JSON or genosha.XML serializations. The syntax for those is much the same as for pickle:

With json:

>>> import genosha.JSON
>>> json_string = genosha.JSON.dumps( obj )
>>> obj_again = genohsa.JSON.loads( json_string )

With xml:

>>> import genosha.XML
>>> xml_string = genosha.XML.dumps( obj )
>>> obj_again = genosha.XML.loads( xml_string )

It is possible to write additional input/output functions, of course. In that case you will probably want to look at the GenoshaEncoder and GenoshaDecoder classes.

Dependencies

Python 2.6 or Python 2.5 with simplejson.

License

GPL v3

Authors

Shawn Sulma (unayok@470th.org)

Contact

Shawn Sulma (unayok@470th.org)

Download

You can download this project in either zip or tar formats.

You can also clone the project with Git by running:

$ git clone git://github.com/unayok/genosha