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 error handling by kcarnold 4 years, 11 months ago
  2. Ajax API class by kcarnold 4 years, 11 months ago
  3. Deep json serialization by Alexey Boriskin 3 years, 10 months ago
  4. Admob Ad Insertion Snippet by johnboxall 4 years, 10 months ago
  5. Format transition middleware by limodou 6 years, 2 months ago

Comments

dan90 (on September 15, 2008):

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

#

(Forgotten your password?)