summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSambhav Satija <sambhav13085@iiitd.ac.in>2015-08-12 06:23:26 +0530
committerTim Graham <timograham@gmail.com>2015-08-12 10:39:07 -0400
commitd0bd5330432e1dda519ebd89606bd0980a36dcb4 (patch)
treed40cb0e19bb24c43e5f4c67b47b171b45f240e29
parent290145e6616b32a0b0c63f44b7c09d0a4af7dfd5 (diff)
Fixed #25254 -- Added JsonResponse json_dumps_params parameter.
-rw-r--r--django/http/response.py8
-rw-r--r--docs/ref/request-response.txt9
-rw-r--r--docs/releases/1.9.txt4
-rw-r--r--tests/httpwrappers/tests.py4
4 files changed, 22 insertions, 3 deletions
diff --git a/django/http/response.py b/django/http/response.py
index aece5ec5a6..b42f0b51d3 100644
--- a/django/http/response.py
+++ b/django/http/response.py
@@ -492,12 +492,16 @@ class JsonResponse(HttpResponse):
``django.core.serializers.json.DjangoJSONEncoder``.
:param safe: Controls if only ``dict`` objects may be serialized. Defaults
to ``True``.
+ :param json_dumps_params: A dictionary of kwargs passed to json.dumps().
"""
- def __init__(self, data, encoder=DjangoJSONEncoder, safe=True, **kwargs):
+ def __init__(self, data, encoder=DjangoJSONEncoder, safe=True,
+ json_dumps_params=None, **kwargs):
if safe and not isinstance(data, dict):
raise TypeError('In order to allow non-dict objects to be '
'serialized set the safe parameter to False')
+ if json_dumps_params is None:
+ json_dumps_params = {}
kwargs.setdefault('content_type', 'application/json')
- data = json.dumps(data, cls=encoder)
+ data = json.dumps(data, cls=encoder, **json_dumps_params)
super(JsonResponse, self).__init__(content=data, **kwargs)
diff --git a/docs/ref/request-response.txt b/docs/ref/request-response.txt
index 86cc4ccbd1..203db54eb0 100644
--- a/docs/ref/request-response.txt
+++ b/docs/ref/request-response.txt
@@ -912,7 +912,7 @@ types of HTTP responses. Like ``HttpResponse``, these subclasses live in
JsonResponse objects
====================
-.. class:: JsonResponse(data, encoder=DjangoJSONEncoder, safe=True, **kwargs)
+.. class:: JsonResponse(data, encoder=DjangoJSONEncoder, safe=True, json_dumps_params=None, **kwargs)
An :class:`HttpResponse` subclass that helps to create a JSON-encoded
response. It inherits most behavior from its superclass with a couple
@@ -934,6 +934,13 @@ JsonResponse objects
``dict`` instances are allowed). If ``safe`` is ``True`` and a non-``dict``
object is passed as the first argument, a :exc:`TypeError` will be raised.
+ The ``json_dumps_params`` parameter is a dictionary of keyword arguments
+ to pass to the ``json.dumps()`` call used to generate the response.
+
+ .. versionchanged:: 1.9
+
+ The ``json_dumps_params`` argument was added.
+
Usage
-----
diff --git a/docs/releases/1.9.txt b/docs/releases/1.9.txt
index f6ff0e6105..be5c3427a1 100644
--- a/docs/releases/1.9.txt
+++ b/docs/releases/1.9.txt
@@ -545,6 +545,10 @@ Requests and Responses
* Added :meth:`HttpRequest.get_port() <django.http.HttpRequest.get_port>` to
fetch the originating port of the request.
+* Added the ``json_dumps_params`` parameter to
+ :class:`~django.http.JsonResponse` to allow passing keyword arguments to the
+ ``json.dumps()`` call used to generate the response.
+
Tests
^^^^^
diff --git a/tests/httpwrappers/tests.py b/tests/httpwrappers/tests.py
index 74a8f923cf..9ccd66ddf0 100644
--- a/tests/httpwrappers/tests.py
+++ b/tests/httpwrappers/tests.py
@@ -504,6 +504,10 @@ class JsonResponseTests(SimpleTestCase):
response = JsonResponse({}, encoder=CustomDjangoJSONEncoder)
self.assertEqual(json.loads(response.content.decode()), {'foo': 'bar'})
+ def test_json_response_passing_arguments_to_json_dumps(self):
+ response = JsonResponse({'foo': 'bar'}, json_dumps_params={'indent': 2})
+ self.assertEqual(response.content.decode(), '{\n "foo": "bar"\n}')
+
class StreamingHttpResponseTests(SimpleTestCase):
def test_streaming_response(self):