Thursday, July 31st, 2008

JSON Pickle: Serialize your complex Python objects to JSON

Category: JSON, Python

<>p>John Paulett wanted to be able to define complex Python model objects, then seamlessly pass them into CouchDB and to client-side Javascript.

To make this happen for objects that are beyond primitive sets he created JSON Pickle which has been used on the Universal Feed Parser, and lets you do the following:

  1. >>> import jsonpickle
  2. >>> from jsonpickle.tests.classes import Thing
  3.  
  4. # Create an object.
  5. >>> obj = Thing('A String')
  6. >>> print obj.name
  7. A String
  8.  
  9. # Use jsonpickle to transform the object into a JSON string.
  10. >>> pickled = jsonpickle.dumps(obj)
  11. >>> print pickled
  12. {"child": null, "classname__": "Thing", "name": "A String", "classmodule__": "jsonpickle.tests.classes"}
  13.  
  14. # Use jsonpickle to recreate a Python object from a JSON string
  15. >>> unpickled = jsonpickle.loads(pickled)
  16. >>> print unpickled.name
  17. A String

Related Content:

Posted by Dion Almaer at 7:50 am
1 Comment

+++--
3.7 rating from 25 votes

1 Comment »

Comments feed TrackBack URI

nice work!

definitely see a use for this

Comment by indiehead — August 1, 2008

Leave a comment

You must be logged in to post a comment.