Question: How to make litestar apply msgspec custom enc_hook / dec_hook functions to serialize/deserialize custom types? #3584
-
Using msgspec for modelling my entities, I need to control serialization/deserialization for some custom types. Msgspec provides a mechanism for this, which involves writing custom enc_hook / dec_hook functions (see msgspec extending docs), which then can be passed to the encoding/decoding methods of msgspec. My question is: When I now want to use these models in litestar, where / how can I tell litestar to use my custom enc_hook / dec_hook functions? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
You can pass https://docs.litestar.dev/latest/reference/app.html#litestar.app.Litestar.params.type_encoders |
Beta Was this translation helpful? Give feedback.
You can pass
type_encoders
/type_decoders
on every layer. These allow you to define custom msgspec hooks.https://docs.litestar.dev/latest/reference/app.html#litestar.app.Litestar.params.type_encoders