Commits

Simone Marzola committed d03648e

made possible to serve json strings using @expose('json') decorator

Comments (0)

Files changed (1)

artichoke/controller.py

 import inspect, pickle, base64, types, urllib
 from thread_locals import ThreadLocalProxy
 from template import ArtichokeTemplateLoader
+try:
+    import json
+except ImportError:
+    import simplejson as json
 
 request = ThreadLocalProxy()
 response = ThreadLocalProxy()
         if not hasattr(f, 'artichoke'):
             f.artichoke = {}
 
-        f.artichoke['template'] = template
+        f.artichoke['template'] = template if content_type == 'text/html' else None
         f.artichoke['content-type'] = content_type
         return f
     return decorate
                 members = dict(inspect.getmembers(call))
                 try:
                     call_candidate = members[subpath]
-                    if isinstance(call_candidate, Controller) or \
+                    if isinstance(call_candidate, Controller) or\
                        (hasattr(call_candidate, 'exposed') and call_candidate.exposed):
                         call = call_candidate
                         path = path[1:]
 
             template = call.artichoke['template']
             response.body = self.render(template, tmpl_context)
+        elif call.artichoke['content-type'] == 'application/json':
+            response.body = json.dumps(call(path, request.params))
         else:
             response.body = call(path, request.params)