Ticket #25468: 25468.diff

File 25468.diff, 2.0 KB (added by Claude Paroz, 9 years ago)
  • django/core/serializers/json.py

    diff --git a/django/core/serializers/json.py b/django/core/serializers/json.py
    index e023997..4c963b0 100644
    a b from django.core.serializers.python import (  
    1616    Deserializer as PythonDeserializer, Serializer as PythonSerializer,
    1717)
    1818from django.utils import six
     19from django.utils.functional import Promise
    1920from django.utils.timezone import is_aware
    2021
    2122
    class DjangoJSONEncoder(json.JSONEncoder):  
    111112            return str(o)
    112113        elif isinstance(o, uuid.UUID):
    113114            return str(o)
     115        elif isinstance(o, Promise):
     116            return six.text_type(o)
    114117        else:
    115118            return super(DjangoJSONEncoder, self).default(o)
    116119
  • tests/serializers/test_json.py

    diff --git a/tests/serializers/test_json.py b/tests/serializers/test_json.py
    index 4239dce..d8d445d 100644
    a b import re  
    66
    77from django.core import serializers
    88from django.core.serializers.base import DeserializationError
    9 from django.test import TestCase, TransactionTestCase
     9from django.core.serializers.json import DjangoJSONEncoder
     10from django.test import SimpleTestCase, TestCase, TransactionTestCase
     11from django.utils.translation import override, ugettext_lazy
    1012
    1113from .models import Score
    1214from .tests import SerializersTestBase, SerializersTransactionTestBase
    class JsonSerializerTransactionTestCase(SerializersTransactionTestBase, Transact  
    271273            "name": "Agnes"
    272274        }
    273275    }]"""
     276
     277
     278class DjangoJSONEncoderTests(SimpleTestCase):
     279    def test_lazy_string_encoding(self):
     280        self.assertEqual(
     281            json.dumps({'lang': ugettext_lazy("French")}, cls=DjangoJSONEncoder),
     282            '{"lang": "French"}'
     283        )
     284        with override('fr'):
     285            self.assertEqual(
     286                json.dumps({'lang': ugettext_lazy("French")}, cls=DjangoJSONEncoder),
     287                '{"lang": "Fran\\u00e7ais"}'
     288            )
Back to Top