Skip to content

Mapping an Option through a serialize_with function #658

Answered by jonasbb
jwodder asked this question in Q&A
Discussion options

You must be logged in to vote

This is exactly what serde_with helps with. I don't know why you think that serializing an Option<T> is a problem. There is even special support to make working with Option simpler and more intuitive: https://docs.rs/serde_with/3.4.0/serde_with/guide/serde_as/index.html#deserializing-optional-fields

The bottom example that you call "abuse" is basically what you have to do, except you can't use a function as a type like this. So you have to use a new type and implement SerializeAs, but that is just some boilerplate syntax changes. Check out the user guide for how to implement SerializeAs. It is very simple since you already have the function: https://docs.rs/serde_with/3.4.0/serde_with/gui…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by jwodder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants