Skip to content

Instantly share code, notes, and snippets.

@vstoykov
Last active January 4, 2016 08:39
Show Gist options
  • Save vstoykov/8596778 to your computer and use it in GitHub Desktop.
Save vstoykov/8596778 to your computer and use it in GitHub Desktop.
try:
import json
except ImportError:
from django.utils import simplejson as json
from django.http import HttpResponse
from django.conf import settings
from django.core.serializers.json import DjangoJSONEncoder
JSON_CONTENT_TYPE = 'application/json; charset=%s' % (settings.DEFAULT_CHARSET, )
JSON_FALLBACK_CONTENT_TYPE = "text/plain; charset=%s" % (settings.DEFAULT_CHARSET, )
class JSONResponse(HttpResponse):
"""
Response that will return JSON serialized value of the content.
It accept request as first argument because it need set correct
contenty type for older version of IE.
"""
INDENT = 1 if settings.DEBUG else None
def __init__(self, request, content, *args, **kwargs):
accept = request.META.get('HTTP_ACCEPT', '*/*')
if 'text/html' in accept and 'application/json' not in accept:
content_type = JSON_FALLBACK_CONTENT_TYPE
else:
content_type = JSON_CONTENT_TYPE
json_content = json.dumps(content, cls=DjangoJSONEncoder, indent=self.INDENT)
super(JSONResponse, self).__init__(json_content, content_type, *args, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment