Skip to content

Latest commit

 

History

History
30 lines (25 loc) · 570 Bytes

attr-rename.md

File metadata and controls

30 lines (25 loc) · 570 Bytes

Serialize fields as camelCase

!PLAYGROUND b2852ed8e696999ccd9d2ac668b848bf

use serde::Serialize;

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
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:
    //
    //    {
    //      "firstName": "Graydon",
    //      "lastName": "Hoare"
    //    }
    println!("{}", json);
}