expression

Serde alias: accept an old field name

Accept a legacy field name on input while emitting the canonical name on output. It is useful for deliberate schema migration. Serde and serde_json are external crates; use the staged Cargo manifest. Expected stdout (not executed): {"name":"Nima"} Common mistake: Expecting alias to serialize both names.

Formal Syntax & Structure

rust syntaxrust
#[serde(alias = "old_name")]

Working Example

Example Snippetrust
fn main() {    #[derive(serde::Serialize, serde::Deserialize)]    struct Profile { #[serde(alias = "nickname")] name: String }    let p: Profile = serde_json::from_str(r#"{"nickname":"Nima"}"#).unwrap();    println!("{}", serde_json::to_string(&p).unwrap());}
Rust Reference