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.  

Source: Ajaxian » Front Page
Original Article: http://feeds.feedburner.com/~r/ajaxian/~3/351507664/json-pickle-serialize-your-complex-python-objects-to-json

Comments are closed.



Site Navigation