1
from django.core.serializers import serialize
2
from django.http import HttpResponse
3
from django.utils import simplejson
4
from django.utils.functional import Promise
5
from django.utils.encoding import force_unicode
7
class LazyEncoder(simplejson.JSONEncoder):
8
def default(self, obj):
9
if isinstance(obj, Promise):
10
return force_unicode(obj)
13
class JSONResponse(HttpResponse):
15
A simple subclass of ``HttpResponse`` which makes serializing to JSON easy.
17
def __init__(self, object, is_iterable = True):
19
content = serialize('json', object)
21
content = simplejson.dumps(object, cls=LazyEncoder)
22
super(JSONResponse, self).__init__(content, mimetype='application/json')
24
class XMLResponse(HttpResponse):
26
A simple subclass of ``HttpResponse`` which makes serializing to XML easy.
28
def __init__(self, object, is_iterable = True):
30
content = serialize('xml', object)
33
super(XMLResponse, self).__init__(content, mimetype='application/xml')
b'\\ No newline at end of file'