-
-
Notifications
You must be signed in to change notification settings - Fork 778
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add struct-level annotation for renaming fields #140
Comments
This was implemented a while ago. |
does this feature still exist? I can't seem to find it in the docs. |
@williamho figuring out how to do this was surprisingly tricky, as the syntax apparently changed a few times, and it really doesn't seem to be documented anywhere. This is what worked for me: #[derive(Debug, Deserialize)]
pub struct UploadResponse {
fid: String,
#[serde(rename="fileUrl")]
file_url: String
} |
@scottpleb your specific use case is documented here: https://serde.rs/attr-rename.html All the attributes are documented here: https://serde.rs/attributes.html |
I don't think this was ever implemented. |
@46bit mentioned possibly being interested in working on this. Possibly the easiest implementation would be right above this line, traverse the "body" and apply all of the renames in place. So if we have the item attrs asking for camelCase, we would change all of the names of the fields (except those that already have a rename) to be camelCase. Alternatively you could take a reference to this guy or just its bulk rename field, and pass it through these1 two2 calls so the field names get set correctly right away rather than having to update them later. Try whichever one seems easier to you. In terms of the actual attributes, I don't like @erickt's #[serde(rename_all = "snake_case")]
#[serde(rename_all = "kebab-case")]
#[serde(rename_all = "SNAKE_CASE")]
#[serde(rename_all = "camelCase")]
#[serde(rename_all = "PascalCase")] Bonus points for not even looking at the word, just the casing. #[serde(rename_all = "easterEggCase")] |
Awesome. I like The obvious signature would be a mapping from field name to output name, |
After doing some implementation, I don't know if renaming by function is possible without wider changes. There exists the Inflector crate to perform inflection. That adds a dependency and relies on |
Implemented in #788. |
Serde supports field level renaming with:
But this can be inconvenient to use if you have a lot of fields you need to rename, such as going from rust's "snake-case" style into "camel-case". It would be nice to have struct level renaming support for this:
Would get serialized to
fooBarBaz
, andfoo-bar-baz
.The text was updated successfully, but these errors were encountered: