Thursday, August 21, 2008

jsonpickle 0.1.0 released

Thanks to Adam for finding the bug that missed longs as a primitive type.

In addition to that quick bugfix, jsonpickle has undergone a major API change. At the core of this change is jsonpickle's new preference for python-cjson over simplejson. While python-cjson is not a requirement, if it is installed on the machine, jsonpickle will use python-cjson. python-cjson is faster for jsonpickle (see the benchmarks), but simplejson is still useful on Google's App Engine.

So the API change moves away from the load/loads/dump/dumps functions in the tradition of simplejson towards a simple encode/decode set of functions. If you need to save the output or read a file, you can do that on your own, then send that data into the encode/decode methods. While it may have been smart to deprecate these functions first, I want to get the API correct early on and prevent cruft.

The update example:

>>> 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.encode(obj)
>>> print pickled
{"classname__": "Thing", "child": null, "name": "A String", "classmodule__": "jsonpickle.tests.classes"}

Use jsonpickle to recreate a Python object from a JSON string
>>> unpickled = jsonpickle.decode(pickled)
>>> print unpickled.name
A String


Grab the new version.

Thanks also go out to the Joose developers for integrating jsonpickle into their product!

Friday, August 8, 2008

Announcing stopwatch 0.3.0 and the 7 Oars code repository

The 7 Oars code repository is a centralized location for small python programs and modules from 7 Oars, instead of having multiple wikis and source control systems for relatively small projects.

The first module as part of the code repository is stopwatch. stopwatch is a very simple python module for measuring time. I typically use it to measure the execution time of specifc parts of code or data processing threads. For example:


>>> import stopwatch
>>> t = stopwatch.Timer() # immediately starts the clock
>>> t.elapsed # elapsed time in seconds
0.2
>>> t.elapsed
1.6
>>> str(t) # pretty print
1.8 sec
>>> for i in xrange(0, 10000)
>>> pass
>>> t.elapsed # still going
10.4
>>> t.stop() # stop the timer
>>> t.elapsed
10.6
>>> t.elapsed # it will not go any more
10.6

Download: http://code.google.com/p/7oars/downloads/list

via PyPi:
easy_install -U stopwatch


via subversion:
svn checkout http://7oars.googlecode.com/svn/trunk/stopwatch stopwatch

Monday, July 21, 2008

jsonpickle 0.0.5 released

Thanks to Dean for pointing the incompatibility with CouchDB keys. This release breaks backwards compatibility by removing the two leading underscores from the special "class*__" keys.


Install / Upgrade:

The easiest path is with PyPi:
easy_install -U jsonpickle

Other installation candidates are on Google.


This release also marks a switch to Google Code's Subversion repository:
svn checkout http://jsonpickle.googlecode.com/svn/trunk/ jsonpickle

Feel free to help out or find bugs!

Thursday, June 12, 2008

jsonpickle 0.0.4 released

The 0.0.4 release of jsonpickle is a bugfix release. One user, Ian Kallen, noted an issue using Paver, so we have switched back to regular setuptools. Thanks Ian! If you have 0.0.2 already installed, you do not have to upgrade.

You can download the new release via
easy_install -U jsonpickle

or from google:
http://code.google.com/p/jsonpickle/downloads/list

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!

Friday, January 25, 2008

Ad Initium

"At the beginning."