CherryPy Project Download

root/jsonrender/__init__.py

Revision 4 (checked in by jtate, 2 years ago)

Add a jsonrender tool

Line 
1 # Copyright (c) 2007 CherryPy
2 # Released under the same license as CherryPy
3
4 # To use:
5 # cherrypy.tools.jsonrender = JsonRender()
6 #
7 # class foo(object):
8 #     @cherrypy.expose
9 #     @cherrypy.tools.jsonrender()
10 #     def index(self):
11 #         return dict(foo=['complication', dict(a='b')])
12
13 import cherrypy
14
15 import simplejson
16
17 class JsonRender(cherrypy.Tool):
18     def __init__(self, name=None, priority=0):
19         self._point = 'before_handler'
20         self._name=name
21         self._priority = priority
22
23     def callable(self):
24         cherrypy.request._handler = cherrypy.request.handler
25         def wrap(*args, **kwargs):
26             ret = cherrypy.request._handler(*args, **kwargs)
27             return simplejson.dumps(ret)
28         cherrypy.request.handler = wrap
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

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