[1.11.x] Fixed #28230 -- Allowed DjangoJsonEncoder to serialize CallableBool.

This commit is contained in:
vinay karanam 2017-05-23 07:28:38 +05:30 committed by Tim Graham
parent 65dfe579d0
commit d1d08d86ba
3 changed files with 11 additions and 0 deletions

View File

@ -16,6 +16,7 @@ from django.core.serializers.python import (
Deserializer as PythonDeserializer, Serializer as PythonSerializer,
)
from django.utils import six
from django.utils.deprecation import CallableBool
from django.utils.duration import duration_iso_string
from django.utils.functional import Promise
from django.utils.timezone import is_aware
@ -117,5 +118,7 @@ class DjangoJSONEncoder(json.JSONEncoder):
return str(o)
elif isinstance(o, Promise):
return six.text_type(o)
elif isinstance(o, CallableBool):
return bool(o)
else:
return super(DjangoJSONEncoder, self).default(o)

View File

@ -29,3 +29,6 @@ Bugfixes
* Fixed a regression where ``Model._state.adding`` wasn't set correctly on
multi-table inheritance parent models after saving a child model
(:ticket:`28210`).
* Allowed ``DjangoJSONEncoder`` to serialize
``django.utils.deprecation.CallableBool`` (:ticket:`28230`).

View File

@ -12,6 +12,7 @@ from django.core.serializers.json import DjangoJSONEncoder
from django.db import models
from django.test import SimpleTestCase, TestCase, TransactionTestCase
from django.test.utils import isolate_apps
from django.utils.deprecation import CallableFalse, CallableTrue
from django.utils.translation import override, ugettext_lazy
from .models import Score
@ -315,3 +316,7 @@ class DjangoJSONEncoderTests(SimpleTestCase):
json.dumps({'duration': duration}, cls=DjangoJSONEncoder),
'{"duration": "P0DT00H00M00S"}'
)
def test_callable_bool(self):
self.assertEqual(json.dumps(CallableTrue, cls=DjangoJSONEncoder), 'true')
self.assertEqual(json.dumps(CallableFalse, cls=DjangoJSONEncoder), 'false')