Ticket #13182: 13182.json-white-spaces.diff

File 13182.json-white-spaces.diff, 1.9 KB (added by Julien Phalip, 14 years ago)
  • django/core/serializers/json.py

    diff --git a/django/core/serializers/json.py b/django/core/serializers/json.py
    index b8119f5..1770afc 100644
    a b class Serializer(PythonSerializer):  
    1818    internal_use_only = False
    1919
    2020    def end_serialization(self):
     21        if self.options.get('indent'):
     22            # The default is (', ', ': '). To eliminate useless whitespaces
     23            # after the comma at the end of each line, the representation is
     24            # changed to (',', ': ') only if indent is used.
     25            self.options['separators'] = (',', ': ')
    2126        simplejson.dump(self.objects, self.stream, cls=DjangoJSONEncoder, **self.options)
    2227
    2328    def getvalue(self):
  • tests/modeltests/serializers/tests.py

    diff --git a/tests/modeltests/serializers/tests.py b/tests/modeltests/serializers/tests.py
    index 013438e..d03b08b 100644
    a b class JsonSerializerTestCase(SerializersTestBase, TestCase):  
    360360                ret_list.append(obj_dict["fields"][field_name])
    361361        return ret_list
    362362
     363    def test_indentation_whitespace(self):
     364        """ Ensure that there are no extraneous spaces after the comma at the
     365            end of each line.
     366            Refs #13182.
     367        """
     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        s = Serializer()
     373        self.assertEqual(s.serialize(qset,indent=2), """[
     374  {
     375    "pk": 1,
     376    "model": "serializers.score",
     377    "fields": {
     378      "score": 5.0
     379    }
     380  },
     381  {
     382    "pk": 2,
     383    "model": "serializers.score",
     384    "fields": {
     385      "score": 6.0
     386    }
     387  }
     388]""")
     389   
    363390class JsonSerializerTransactionTestCase(SerializersTransactionTestBase, TransactionTestCase):
    364391    serializer_name = "json"
    365392    fwd_ref_str = """[
Back to Top