0PricingLogin
Learn Rust Coding · Lesson

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

  1. Serialize and Deserialize
  2. Working with JSON
  3. Custom Serialization
  4. Other Formats
← Back to Learn Rust Coding