1
from django.core.serializers import serialize
2
from django.http import HttpResponse
3
from django.utils.functional import Promise
4
from django.utils.encoding import force_text
5
import json as simplejson
8
class LazyEncoder(simplejson.JSONEncoder):
10
def default(self, obj):
11
if isinstance(obj, Promise):
12
return force_text(obj)
16
class JSONResponse(HttpResponse):
17
"""A simple subclass of ``HttpResponse`` which makes serializing to JSON
20
def __init__(self, object, is_iterable=True):
22
content = serialize('json', object)
24
content = simplejson.dumps(object, cls=LazyEncoder)
25
super(JSONResponse, self).__init__(
26
content, mimetype='application/json')
29
class XMLResponse(HttpResponse):
30
"""A simple subclass of ``HttpResponse`` which makes serializing to XML
33
def __init__(self, object, is_iterable=True):
35
content = serialize('xml', object)
38
super(XMLResponse, self).__init__(content, mimetype='application/xml')