Skip to content
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 support of dot.case and SCREAMING.DOT.CASE rename on variant and field #2269

Closed
wants to merge 2 commits into from

Conversation

qboileau
Copy link

Add support of Dotcase and CapitalDotcase for compatibility with scala libs enumeratum serialization (using circe for example).

Idea is to add dot.case and SCREAMING.DOT.CASE to rename_all RenameRule for Serialize and Deserialize fields and variants.

use serde::Serialize;

#[derive(Serialize)]
#[serde(rename_all = "dot.case")]
struct Person {
    first_name: String,
    last_name: String,
}

fn main() {
    let person = Person {
        first_name: "Graydon".to_string(),
        last_name: "Hoare".to_string(),
    };

    let json = serde_json::to_string_pretty(&person).unwrap();

    // Prints:
    //    {
    //      "first.name": "Graydon",
    //      "last.name": "Hoare"
    //    }
    println!("{}", json);
}

I'm not sure if that's all that is need to do or if it's missing additional changes.

@qboileau
Copy link
Author

@dtolnay do you need something else for this PR to be merged ?

@Property404
Copy link

This would be nice to have. I was going to push pretty much the same PR but I guess you beat me to it

Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

I would prefer not to build this into serde_derive. But I would be open to someone making an attribute macro (in a different crate) that supports applying a broader set of rename rules to a serde data structure beyond the ones built into serde_derive, by expanding to multiple individual serde(rename = "...") attributes in the right places.

@dtolnay dtolnay closed this Jul 26, 2023
@Mingun
Copy link
Contributor

Mingun commented Jul 27, 2023

What about to allow specify path to the const fn that would perform rename?

const fn rename(name: &str) -> &str { ... }

#[serde(rename_all = path::to::rename)]
struct MyStruct { ... }

Then almost all needs would be covered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants