JSON encoding middleware

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from snippet 801 import JsonResponse
class JsonEncodingMiddleware(object):
    '''If a view returns a dict, encode it as JSON.

    This should be the last middleware in MIDDLEWARE_CLASSES.'''
    def process_response(self, request, response):
        if isinstance(response, dict):
            return JsonResponse(response)
        else:
            return response

More like this

  1. Ajax API class by kcarnold 3 years, 8 months ago
  2. Ajax error handling by kcarnold 3 years, 8 months ago
  3. SerializedObjectField by dominno 1 year, 11 months ago
  4. JSON encode ISO UTC datetime by japerk 2 years, 10 months ago
  5. Extended JSON encoder by kcarnold 3 years, 8 months ago

Comments

dan90 (on September 15, 2008):

hm - one problem here is it should also encode lists and tuples, no?

#

(Forgotten your password?)