@@ -0,0 +1,929 @@
+# -*- coding: utf-8 -*-%0Aimport json%0A%0Afrom django.core.serializers.json import DjangoJSONEncoder%0Afrom django.http import HttpResponse%0A%0A%0Aclass JSONResponseMixin(object):%0A%09%22%22%22%0A%09A mixin that can be used to render a JSON response.%0A%09%22%22%22%0A%09response_class = HttpResponse%0A%0A%09def render_to_response(self, context, **response_kwargs):%0A%09%09%22%22%22%0A%09%09Returns a JSON response, transforming 'context' to make the payload.%0A%09%09%22%22%22%0A%09%09response_kwargs%5B'content_type'%5D = 'application/json'%0A%09%09return self.response_class(%0A%09%09%09%09self.convert_context_to_json(context),%0A%09%09%09%09**response_kwargs%0A%09%09%09%09)%0A%0A%09def convert_context_to_json(self, context):%0A%09%09%22Convert the context dictionary into a JSON object%22%0A%09%09# Note: This is *EXTREMELY* naive; in reality, you'll need%0A%09%09# to do much more complex handling to ensure that arbitrary%0A%09%09# objects -- such as Django model instances or querysets%0A%09%09# -- can be serialized as JSON.%0A%09%09return json.dumps(context, cls=DjangoJSONEncoder)%0A

add JSONResponseMixin