Custom Serialization
Field attributes.
Customizing serde
serde gives fine control over how fields map to a format using attributes. You rename fields, skip them, set defaults, and more, all via #[serde(...)].
Renaming a field
Use rename to map a Rust field name to a different key in the output, useful when JSON uses a different convention.
use serde::Serialize;
#[derive(Serialize)]
struct User {
#[serde(rename = "userName")]
user_name: String,
}All lessons in this course
- Serialize and Deserialize
- Working with JSON
- Custom Serialization
- Other Formats