| 1 | diff --git a/django/core/serializers/json.py b/django/core/serializers/json.py
|
|---|
| 2 | index b07aa33..01d44e6 100644
|
|---|
| 3 | --- a/django/core/serializers/json.py
|
|---|
| 4 | +++ b/django/core/serializers/json.py
|
|---|
| 5 | @@ -24,6 +24,11 @@ class Serializer(PythonSerializer):
|
|---|
| 6 | internal_use_only = False
|
|---|
| 7 |
|
|---|
| 8 | def start_serialization(self):
|
|---|
| 9 | + if self.options.get('indent'):
|
|---|
| 10 | + # The default is (', ', ': '). To eliminate useless whitespaces
|
|---|
| 11 | + # after the comma at the end of each line, the representation is
|
|---|
| 12 | + # changed to (',', ': ') only if indent is used.
|
|---|
| 13 | + self.options['separators'] = (',', ': ')
|
|---|
| 14 | if json.__version__.split('.') >= ['2', '1', '3']:
|
|---|
| 15 | # Use JS strings to represent Python Decimal instances (ticket #16850)
|
|---|
| 16 | self.options.update({'use_decimal': False})
|
|---|
| 17 | @@ -37,8 +42,6 @@ class Serializer(PythonSerializer):
|
|---|
| 18 | if self.options.get("indent"):
|
|---|
| 19 | self.stream.write("\n")
|
|---|
| 20 | self.stream.write("]")
|
|---|
| 21 | - if self.options.get("indent"):
|
|---|
| 22 | - self.stream.write("\n")
|
|---|
| 23 |
|
|---|
| 24 | def end_object(self, obj):
|
|---|
| 25 | # self._current has the field data
|
|---|
| 26 | diff --git a/tests/serializers/tests.py b/tests/serializers/tests.py
|
|---|
| 27 | index 07c220c..4ddbc5c 100644
|
|---|
| 28 | --- a/tests/serializers/tests.py
|
|---|
| 29 | +++ b/tests/serializers/tests.py
|
|---|
| 30 | @@ -392,6 +392,32 @@ class JsonSerializerTestCase(SerializersTestBase, TestCase):
|
|---|
| 31 | ret_list.append(obj_dict["fields"][field_name])
|
|---|
| 32 | return ret_list
|
|---|
| 33 |
|
|---|
| 34 | +
|
|---|
| 35 | +class TestJsonSerializerIndentation(TestCase):
|
|---|
| 36 | + def test_indentation_whitespace(self):
|
|---|
| 37 | + from django.core.serializers.json import Serializer
|
|---|
| 38 | + Score.objects.create(score=5.0)
|
|---|
| 39 | + Score.objects.create(score=6.0)
|
|---|
| 40 | + qset = Score.objects.all()
|
|---|
| 41 | +
|
|---|
| 42 | + s = Serializer()
|
|---|
| 43 | + self.assertEqual(s.serialize(qset, indent=2),"""[
|
|---|
| 44 | +{
|
|---|
| 45 | + "pk": 1,
|
|---|
| 46 | + "model": "serializers.score",
|
|---|
| 47 | + "fields": {
|
|---|
| 48 | + "score": 5.0
|
|---|
| 49 | + }
|
|---|
| 50 | +},
|
|---|
| 51 | +{
|
|---|
| 52 | + "pk": 2,
|
|---|
| 53 | + "model": "serializers.score",
|
|---|
| 54 | + "fields": {
|
|---|
| 55 | + "score": 6.0
|
|---|
| 56 | + }
|
|---|
| 57 | +}
|
|---|
| 58 | +]""")
|
|---|
| 59 | +
|
|---|
| 60 | class JsonSerializerTransactionTestCase(SerializersTransactionTestBase, TransactionTestCase):
|
|---|
| 61 | serializer_name = "json"
|
|---|
| 62 | fwd_ref_str = """[
|
|---|