When to use custom serialisation/deserialisation versus serialise_with/deserialise_with attribute in serde

Going by this section of the serde book here it is possible to implement custom serialisation/deserialisation.

Then recently, going through the docs I found the field attributes serialize_with and deserialize_with which looks like it enables you to also have custom serialisation/deserialisation.

So what are the advantages of one over the other?

Writing the custom implementations means that they will be used everywhere throughout your code, while using the x_with annotations gives you more flexibility (i.e. you could serialize your type differently in some places).

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.