Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #13182 -- Prevented trailing spaces in indented json output

Thanks Stéphane Raimbault for the report and the initial patch.
  • Loading branch information...
commit 3e34005b1b4a9a447e3b3fd76e71f0c73d530414 1 parent 9885f07
Claude Paroz authored August 31, 2013
3  django/core/serializers/json.py
@@ -31,6 +31,9 @@ def start_serialization(self):
31 31
         self.json_kwargs = self.options.copy()
32 32
         self.json_kwargs.pop('stream', None)
33 33
         self.json_kwargs.pop('fields', None)
  34
+        if self.options.get('indent'):
  35
+            # Prevent trailing spaces
  36
+            self.json_kwargs['separators'] = (',', ': ')
34 37
         self.stream.write("[")
35 38
 
36 39
     def end_serialization(self):
25  tests/serializers/tests.py
@@ -392,6 +392,31 @@ def _get_field_values(serial_str, field_name):
392 392
                 ret_list.append(obj_dict["fields"][field_name])
393 393
         return ret_list
394 394
 
  395
+    def test_indentation_whitespace(self):
  396
+        Score.objects.create(score=5.0)
  397
+        Score.objects.create(score=6.0)
  398
+        qset = Score.objects.all()
  399
+
  400
+        s = serializers.json.Serializer()
  401
+        self.assertEqual(s.serialize(qset, indent=2), """[
  402
+{
  403
+  "pk": 1,
  404
+  "model": "serializers.score",
  405
+  "fields": {
  406
+    "score": 5.0
  407
+  }
  408
+},
  409
+{
  410
+  "pk": 2,
  411
+  "model": "serializers.score",
  412
+  "fields": {
  413
+    "score": 6.0
  414
+  }
  415
+}
  416
+]
  417
+""")
  418
+
  419
+
395 420
 class JsonSerializerTransactionTestCase(SerializersTransactionTestBase, TransactionTestCase):
396 421
     serializer_name = "json"
397 422
     fwd_ref_str = """[
Something went wrong with that request. Please try again.