- alias
Alias some fields in JSON (handle dash in JSON keys)
examples/json/alias-json-fields/data.json
{ "name": "Foo", "rust-version": "1.78.1" }
examples/json/alias-json-fields/src/main.rs
use serde::Deserialize; #[derive(Deserialize, Debug)] #[allow(dead_code)] struct Thing { name: String, // rust-version: String, #[serde(alias = "rust-version")] rust_version: String, } fn main() { let filename = "data.json"; let content = std::fs::read_to_string(filename).unwrap(); let data = serde_json::from_str::<Thing>(&content).expect("JSON parsing error"); println!("{:#?}", data); }
Thing { name: "Foo", rust_version: "1.78.1", }