Expand description

Serialize inner value if Some(T). If None, serialize the unit struct ().

When used in conjunction with skip_serializing_if = "Option::is_none" and default, you can build an optional value by skipping if it is None, or serializing its inner value if Some(T).

Not all serialization formats easily support optional values. While JSON uses the Option type to represent optional values and only serializes the inner part of the Some(), other serialization formats, such as RON, choose to serialize the Some around a value. This helper helps building a truly optional value for such serializers.

Example

#[derive(Deserialize, Serialize)]
struct Doc {
    mandatory: usize,
    #[serde(
        default,                                    // <- important for deserialization
        skip_serializing_if = "Option::is_none",    // <- important for serialization
        with = "::serde_with::rust::unwrap_or_skip",
    )]
    optional: Option<usize>,
}

// Transparently add/remove Some() wrapper
let s = r#"(
    mandatory: 1,
    optional: 2,
)"#;
let v = Doc {
    mandatory: 1,
    optional: Some(2),
};
assert_eq!(v, ron::de::from_str(s).unwrap());
assert_eq!(s, ron::ser::to_string_pretty(&v, pretty_config).unwrap());

// Missing values are deserialized as `None`
// while `None` values are skipped during serialization.
let s = r#"(
    mandatory: 1,
)"#;
let v = Doc {
    mandatory: 1,
    optional: None,
};
assert_eq!(v, ron::de::from_str(s).unwrap());
assert_eq!(s, ron::ser::to_string_pretty(&v, pretty_config).unwrap());

Functions

Deserialize value wrapped in Some(T)

Serialize value if Some(T), unit struct if None