Ticket #5590: django-json-lazy.diff

File django-json-lazy.diff, 1.4 KB (added by Jeremy Dunck, 17 years ago)

Altering json.DjangoJSONEncoder to handle encoding of Promise objects.

  • django/core/serializers/json.py

     
    55import datetime
    66from django.utils import simplejson
    77from django.utils.simplejson import decoder
     8from django.utils.functional import Promise
    89from django.core.serializers.python import Serializer as PythonSerializer
    910from django.core.serializers.python import Deserializer as PythonDeserializer
    1011try:
     
    5758            return o.strftime(self.TIME_FORMAT)
    5859        elif isinstance(o, decimal.Decimal):
    5960            return str(o)
     61        elif isinstance(o, Promise):
     62            # The input is the result of a gettext_lazy() call.
     63            return unicode(o).encode('utf-8', 'strict')
    6064        else:
    6165            return super(DjangoJSONEncoder, self).default(o)
    6266
  • tests/regressiontests/i18n/tests.py

     
    3030>>> s4 = ugettext_lazy('Some other string')
    3131>>> s == s4
    3232False
     33
     34>>> from django.core.serializers import json
     35>>> from django.utils import simplejson
     36>>> s5 = ugettext_lazy('Yet another string')
     37>>> simplejson.dumps(s5, cls=json.DjangoJSONEncoder)
     38'"Yet another string"'
     39
    3340"""
Back to Top