Rename serde fields using the #[serde(rename = "name")] attribute or #[serde(rename_all = "case")] on the struct.
Use the #[serde(rename = "new_name")] attribute on the struct field to change its serialized name.
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct User {
#[serde(rename = "user_name")]
username: String,
}
Alternatively, apply #[serde(rename_all = "snake_case")] to the struct to rename all fields automatically.
Renaming fields with serde tells the serialization library to use a different name when saving or sending your data. It is like giving a file a different label on the outside while keeping the original name inside. You use this when your internal code names don't match the external format requirements.