Module serde_yml::with::nested_singleton_map
source · Expand description
Serialize/deserialize nested enums using a YAML map containing one entry in which the key identifies the variant name.
This function is similar to singleton_map
, but it applies the singleton map representation
recursively to all nested enums within the data structure.
§Example
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum InnerEnum {
Variant1,
Variant2(String),
}
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum OuterEnum {
Variant1(InnerEnum),
Variant2 {
inner: InnerEnum,
},
}
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Example {
#[serde(with = "serde_yml::with::nested_singleton_map")]
field: OuterEnum,
}
let example = Example {
field: OuterEnum::Variant2 {
inner: InnerEnum::Variant2("value".to_string()),
},
};
let yaml = serde_yml::to_string(&example).unwrap();
assert_eq!(yaml, "field:\n Variant2:\n inner:\n Variant2: value\n");
let deserialized: Example = serde_yml::from_str(&yaml).unwrap();
assert_eq!(example, deserialized);
Functions§
- Deserializes a value using the nested singleton map representation.
- Serializes a value using the nested singleton map representation.