Module serde_yml::with::singleton_map_optional
source · Expand description
Serialize/deserialize an optional enum using a YAML map containing one entry in which the key identifies the variant name.
This module is similar to singleton_map
, but it works with optional (Option
) fields.
If the field is Some
, it will be serialized/deserialized using the singleton_map
representation.
If the field is None
, it will be serialized/deserialized as null
.
§Example
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum MyEnum {
Variant1,
Variant2(String),
}
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Example {
#[serde(with = "serde_yml::with::singleton_map_optional")]
field: Option<MyEnum>,
}
let example = Example {
field: Some(MyEnum::Variant2("value".to_string())),
};
let yaml = serde_yml::to_string(&example).unwrap();
assert_eq!(yaml, "field:\n Variant2: value\n");
let deserialized: Example = serde_yml::from_str(&yaml).unwrap();
assert_eq!(example, deserialized);
Functions§
- Deserializes a value using the
singleton_map
representation. - Serializes an optional value using the
singleton_map
representation.