Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
@claudep claudep authored
Showing with 28 additions and 0 deletions.
  1. +3 −0  django/core/serializers/json.py
  2. +25 −0 tests/serializers/tests.py
View
3  django/core/serializers/json.py
@@ -31,6 +31,9 @@ def start_serialization(self):
self.json_kwargs = self.options.copy()
self.json_kwargs.pop('stream', None)
self.json_kwargs.pop('fields', None)
+ if self.options.get('indent'):
+ # Prevent trailing spaces
+ self.json_kwargs['separators'] = (',', ': ')
self.stream.write("[")
def end_serialization(self):
View
25 tests/serializers/tests.py
@@ -392,6 +392,31 @@ def _get_field_values(serial_str, field_name):
ret_list.append(obj_dict["fields"][field_name])
return ret_list
+ def test_indentation_whitespace(self):
+ Score.objects.create(score=5.0)
+ Score.objects.create(score=6.0)
+ qset = Score.objects.all()
+
+ s = serializers.json.Serializer()
+ self.assertEqual(s.serialize(qset, indent=2), """[
+{
+ "pk": 1,
+ "model": "serializers.score",
+ "fields": {
+ "score": 5.0
+ }
+},
+{
+ "pk": 2,
+ "model": "serializers.score",
+ "fields": {
+ "score": 6.0
+ }
+}
+]
+""")
+
+
class JsonSerializerTransactionTestCase(SerializersTransactionTestBase, TransactionTestCase):
serializer_name = "json"
fwd_ref_str = """[
Something went wrong with that request. Please try again.