Ticket #13182: less-whitespace-json-serialize.diff

File less-whitespace-json-serialize.diff, 1.8 KB (added by dpn, 5 years ago)
  • django/core/serializers/json.py

    diff -r 956e97430c68 django/core/serializers/json.py
    a b  
    2121        self.options.pop('stream', None)
    2222        self.options.pop('fields', None)
    2323        self.options.pop('use_natural_keys', None)
     24        if self.options.get('indent'):
     25            # The default is (', ', ': ').  To eliminate useless whitespaces
     26            # after comma, the representation is changed to (',', ': ') only
     27            # if indent is used
     28            self.options['separators'] = (',', ': ')
    2429        simplejson.dump(self.objects, self.stream, cls=DjangoJSONEncoder, **self.options)
    2530
    2631    def getvalue(self):
  • tests/modeltests/serializers/models.py

    diff -r 956e97430c68 tests/modeltests/serializers/models.py
    a b  
    88
    99from decimal import Decimal
    1010from django.db import models
     11from django.test.testcases import TestCase
    1112
    1213class Category(models.Model):
    1314    name = models.CharField(max_length=20)
     
    342343except ImportError:
    343344    pass
    344345
     346
     347
     348_json_expected="""[
     349  {
     350    "pk": 1,
     351    "model": "serializers.score",
     352    "fields": {
     353      "score": 5.0
     354    }
     355  },
     356  {
     357    "pk": 2,
     358    "model": "serializers.score",
     359    "fields": {
     360      "score": 6.0
     361    }
     362  }
     363]"""
     364
     365
     366class TestJsonSerializerIndentation(TestCase):
     367    def test_indentation_whitespace(self):
     368        from django.core.serializers.json import Serializer
     369        Score.objects.create(score=5.0)
     370        Score.objects.create(score=6.0)
     371        qset = Score.objects.all()
     372       
     373        s = Serializer()
     374        self.assertEqual(s.serialize(qset,indent=2),_json_expected)
Back to Top