I don't want to write #[serde(rename_all = "kebab-case")] for every struct

I want to convert my very complex data structure from JSON or YAML that uses kebab case for fields and enum keys to Rust own types with Rust naming convention. Doing so requires writing #[serde(rename_all = "kebab-case")] on top of all structs and enums, which is repetitive and error-prone. Is there a better way?

No, I think this is the least repetitive option.

You can write an ad-hoc macro to reduce code duplication: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=1a38f501a8cc92edd6cb4b3f499134f9

2 Likes

I usually end up writing a simple proc-macro in my projects that applies all the serde attributes I use over and over, including rename_all, deny_unknown_fields, default, skip_serializing_if, etc. That way I can code the logic for which attributes should be applied where however I want.

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.