MCPcopy
hub / github.com/pydantic/pydantic / test_custom_encoder

Function test_custom_encoder

tests/test_json.py:212–226  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

210
211
212def test_custom_encoder():
213 class Model(BaseModel):
214 x: timedelta
215 y: Decimal
216 z: date
217
218 @field_serializer('x')
219 def serialize_x(self, v: timedelta, _info):
220 return f'{v.total_seconds():0.3f}s'
221
222 @field_serializer('y')
223 def serialize_y(self, v: Decimal, _info):
224 return 'a decimal'
225
226 assert Model(x=123, y=5, z='2032-06-01').model_dump_json() == '{"x":"123.000s","y":"a decimal","z":"2032-06-01"}'
227
228
229def test_iso_timedelta_simple():

Callers

nothing calls this directly

Calls 2

model_dump_jsonMethod · 0.80
ModelClass · 0.70

Tested by

no test coverage detected