<reponame>Mozilla-GitHub-Standards/b6d67e88a40f8bf3a3d409382eee26f2b21c3dfdd83a544343ab9629bc3d05d9<gh_stars>10-100
from django.http import HttpResponse
from django.utils import simplejson
from django.utils.functional import Promise
from django.utils.encoding import force_unicode
from django.core.serializers.json import DjangoJSONEncoder

class LazyEncoder(DjangoJSONEncoder):
    def default(self, obj):
        if isinstance(obj, Promise):
            return force_unicode(obj)
        return super(LazyEncoder, self).default(obj)

def render_json_to_response(context):
    '''
    Utility method for rendering a view's data to JSON response.
    '''
    result = simplejson.dumps(context, sort_keys=False, indent=4, cls=LazyEncoder)
    return HttpResponse(result, mimetype='application/javascript')
