From d0bd5330432e1dda519ebd89606bd0980a36dcb4 Mon Sep 17 00:00:00 2001 From: Sambhav Satija Date: Wed, 12 Aug 2015 06:23:26 +0530 Subject: [PATCH] Fixed #25254 -- Added JsonResponse json_dumps_params parameter. --- django/http/response.py | 8 ++++++-- docs/ref/request-response.txt | 9 ++++++++- docs/releases/1.9.txt | 4 ++++ tests/httpwrappers/tests.py | 4 ++++ 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() ` 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):