JSON
Here is a tool that jsonifies the controller output. Note that you need to use CherryPy 3.0.1 or later to use tool decorators with RoutesDispatcher:
from simplejson import JSONEncoder encoder = JSONEncoder() def jsonify_tool_callback(*args, **kwargs): response = cherrypy.response response.headers['Content-Type'] = 'application/json' response.body = encoder.iterencode(response.body) cherrypy.tools.jsonify = cherrypy.Tool('before_finalize', jsonify_tool_callback, priority=30)
The iterencode function of the simplejson JSONEncoder returns a generator that jsonifies the result incrementally (for example if you're jsonifying a large list you start getting json outputs for the first objects right away).
I usually use this by setting _cp_config on a controller class or as a decorator on the method:
from cherrypy import tools class Root(object): @tools.jsonify() def getrange(self, limit=4): return range(limit)
-- Arnar Birgisson
There are some issues with this approach, namely that your page handler must return an iterable or cherrypy.response.body will try to turn it into an iterable for you. A true decorator, or a tool that wrapped the page handler, would be slightly less fragile.
-- Robert Brewer

