On Feb 7, 2006, at 9:40 PM, schwa wrote:
Sorry about the blatant self publicising. I have some code that might
be handy for anyone thinking about directly interfacing a Turbogears
Application with Cocoa (on Mac OS X of course). I've written JSON
serializer/deserializer classes for Cocoa and also have JSON_INPUT
decorator that you can use in your TG controller methods to allow a
method to take input via JSON data in the http body.
I've blogged about here
http://toxicsoftware.com/blog/index.php/weblog/entry/
cocoa_json_turbogears/
and here
http://toxicsoftware.com/blog/index.php/weblog/entry/cocoajson/
Looks interesting, but your decorator is semi-broken.
simplejson.read (and simplejson.write) are deprecated and are going
to spew warnings every time you use them. They're cruft from json-py
compatibility.
The reason they're deprecated is because the methods "read" and
"write" almost always means something entirely different in Python
(e.g. file-like objects). You should be using loads/dumps (the same
API as marshal and pickle).
The most appropriate code would be simply this::
theDictionary = simplejson.load(cherrypy.request.body)
Rather than::
theBody = cherrypy.request.body.read()
theDictionary = simplejson.read(theBody)
Additionally, JSON_INPUT is not a PEP-8 compliant name. It should be
called json_input.
-bob