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