Friday, June 6, 2008

Announcing jsonpickle 0.0.2

jsonpickle, a python library for serializing Python objects to JSON, is now public. The unique feature of jsonpickle is that it will work on almost any existing builtin or custom object without modification. simplejson does include a mechanism for serializing objects by defining a '.default()' method, but this requires modification to the class. Additionally, Christopher Lenz's Python CouchDB wrapper, using the Schema class, allows a class to implement JSON safe fields. While powerful, it still requires an upfront effort and is unable to handle existing code.

Marc Stober proposed using an object's __dict__ to serialize an object. His approach forms the basic tenet of jsonpickle. jsonpickle, though, takes this approach to the extreme, allowing for a much more robust solution, including allowing embedded objects to be serialized.

jsonpickle is currently able to handle a wide-range of Python objects: new-style and old-style classes, dictionaries, lists, time_struct's, even Mark Pilgrim's Universal Feed Parser.


>>> import jsonpickle
>>> from jsonpickle.tests.classes import Thing


Create an object.

>>> obj = Thing('A String')
>>> print obj.name
A String


Use jsonpickle to transform the object into a JSON string

>>> pickled = jsonpickle.dumps(obj)
>>> print pickled
{"child": null, "__classname__": "Thing", "name": "A String", "__classmodule__": "jsonpickle.tests.classes"}


Use jsonpickle to recreate a Python object from a JSON string

>>> unpickled = jsonpickle.loads(pickled)
>>> print unpickled.name
A String


The new object has the same type and data, but essentially is now a copy of the original.

>>> obj == unpickled
False
>>> obj.name == unpickled.name
True
>>> type(obj) == type(unpickled)
True


If you will never need to load (regenerate the Python class from JSON), you can
pass in the keyword unpicklable=False to prevent extra information from being
added to JSON.

>>> oneway = jsonpickle.dumps(obj, unpicklable=False)
>>> print oneway
{"name": "A String", "child": null}


jsonpickle currently extends the functionality of simplejson (future releases may use a different library, such as cjson).

The package is available on PyPi (so you can get it via an 'easy_install jsonpickle'). Additionally, Google Code is hosting the binary distributions of the code (including a Windows installer).

Feel free to help out! We could use reports of what other modules work or do not work with jsonpickle. Submit bug reports. Hack the code (we are currently using Mercurial). Join the mailing list.

One warning: jsonpickle is still alpha level software. The code is tested, but bugs may exist. Additionally, we do not currently guarantee backwards-compatibility yet--so the json output may not be valid to jsonpickle in future releases.

Enjoy!

2 comments:

rajasadeghi said...

Also, do not miss the 10% weekly rebate when you start to play regularly at the on line casino. And if you're a crypto consumer, enter the CRYPTO400 code and luxuriate in a whopping 400% match 우리카지노 아벤카지노 bonus up to as} $4,000 in your first deposit. We additionally wish to praise Red Dog for including a devoted telephone line for support communication. You can contact the support representatives via reside chat or e mail or visit the on-site discussion board.

caitrinexanders said...

You can discover all one of the best on-line casinos in India on this web site. If you suppose this sounds attention-grabbing, we advise you examine out|to take a glance at} our on line casino suggestions and see if yow will discover a fantastic 1xbet korea on line casino that suits your needs and preferences. Finding the ideal website can be tough because of|as a result of} Indian on line casino roulette is so extensively available on the internet. That’s why we’ve decided to help you by offering a sneak peek on the elements we examine when selecting the greatest on-line roulette on line casino.