Module serde_yml::with::singleton_map_with
source · Expand description
Serialize/deserialize an enum using a YAML map containing one entry in which
the key identifies the variant name, while allowing combination with other serialize_with
attributes.
This module provides a way to use singleton_map
in combination with other serialize_with
attributes.
It simply forwards the serialization and deserialization calls to the singleton_map
module.
§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_with")]
field: MyEnum,
}
// Assuming `some_other_module` is defined elsewhere
mod some_other_module {
use serde::{Deserialize, Deserializer, Serialize, Serializer};
pub fn serialize<T, S>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
where
T: Serialize,
S: Serializer,
{
// Custom serialization logic
value.serialize(serializer)
}
pub fn deserialize<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where
T: Deserialize<'de>,
D: Deserializer<'de>,
{
// Custom deserialization logic
T::deserialize(deserializer)
}
}
let example = Example {
field: 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 a value using the
singleton_map
representation.