CherryPy Project Download


Here is a tool that jsonifies the controller output. I usually use this by setting _cp_config on a controller class or as a decorator on the method. Note that you need to use rev. 1597 or later to use tool decorators with RoutesDispatcher

def jsonify_tool_callback(*args, **kwargs):
    response = cherrypy.response
    response.headers['Content-Type'] = 'text/javascript'
    response.body = encode_iter(response.body) = cherrypy.Tool('before_finalize', jsonify_tool_callback, priority=30) 

The encode_iter function calls iterencode on a simplejson JSONEncoder, which 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).

-- Arnar Birgisson

Hosted by WebFaction

Log in as guest/cherrypy to create/edit wiki pages