MCPcopy
hub / github.com/django/django / test_custom_encoder

Method test_custom_encoder

tests/serializers/test_json.py:79–93  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

77
78 @isolate_apps("serializers")
79 def test_custom_encoder(self):
80 class ScoreDecimal(models.Model):
81 score = models.DecimalField()
82
83 class CustomJSONEncoder(json.JSONEncoder):
84 def default(self, o):
85 if isinstance(o, decimal.Decimal):
86 return str(o)
87 return super().default(o)
88
89 s = serializers.json.Serializer()
90 json_data = s.serialize(
91 [ScoreDecimal(score=decimal.Decimal(1.0))], cls=CustomJSONEncoder
92 )
93 self.assertIn('"fields": {"score": "1"}', json_data)
94
95 def test_json_deserializer_exception(self):
96 with self.assertRaises(DeserializationError):

Callers

nothing calls this directly

Calls 2

serializeMethod · 0.95
ScoreDecimalClass · 0.70

Tested by

no test coverage detected