expression
Serde field renaming
Map a Rust field name to an external key for serialization and deserialization. Keep the public wire name stable even if internal names change. Serde and serde_json are external crates; use the staged Cargo manifest. Expected stdout (not executed): {"userId":9} Common mistake: Renaming public keys without a migration plan.
Formal Syntax & Structure
rust syntaxrust
#[serde(rename = "externalName")]Working Example
Example Snippetrust
fn main() { #[derive(serde::Serialize, serde::Deserialize)] struct Account { #[serde(rename = "userId")] user_id: u32 } let item: Account = serde_json::from_str(r#"{"userId":9}"#).unwrap(); println!("{}", serde_json::to_string(&item).unwrap());}