Activate your free membership today | Log-in

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:

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

Related Content:

  • Notable Python language update on view
    Google's interest in Python is just one reason this language continues to gain attention. If you are looking for a scripting language to do some rapid...
  • JSON (Javascript Object Notation)
    JSON (JS Object Notation) is a text-based, human-readable data interchange format used for representing simple data structures and objects in Web...
  • JSON
    JSON (JS Object Notation) is a text-based, human-readable data interchange format used for representing simple data structures and objects in Web...
  • Javascript Object Notation
    JSON (JS Object Notation) is a text-based, human-readable data interchange format used for representing simple data structures and objects in Web...
  • VSLive: ASP.NET AJAX extensions employ JSON serializer
    Among useful traits in ASP.NET AJAX Extensions (formerly Atlas) and the associated tool kit are improved Web service proxy handling and JSON-based...

Posted by Dion Almaer at 7:50 am
1 Comment

+++--
3.5 rating from 22 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.