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

Method test_custom_encoder

tests/serializers/test_jsonl.py:66–81  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

64
65 @isolate_apps("serializers")
66 def test_custom_encoder(self):
67 class ScoreDecimal(models.Model):
68 score = models.DecimalField()
69
70 class CustomJSONEncoder(json.JSONEncoder):
71 def default(self, o):
72 if isinstance(o, decimal.Decimal):
73 return str(o)
74 return super().default(o)
75
76 s = serializers.jsonl.Serializer()
77 json_data = s.serialize(
78 [ScoreDecimal(score=decimal.Decimal(1.0))],
79 cls=CustomJSONEncoder,
80 )
81 self.assertIn('"fields": {"score": "1"}', json_data)
82
83 def test_json_deserializer_exception(self):
84 with self.assertRaises(DeserializationError):

Callers

nothing calls this directly

Calls 2

serializeMethod · 0.95
ScoreDecimalClass · 0.70

Tested by

no test coverage detected