Closed LGUG2Z closed 2 years ago
You would need to use serde(default)
if you want to support the field being absent. serde(with)
requires to be explicit about this because it is not necessarily what you want when replacing the deserialization logic for a type.
#[derive(Serialize, Deserialize)]
struct MyStruct {
required_member: usize,
#[serde(with = "serde_yaml::with::singleton_map", default)]
optional_member: Option<MyEnum>,
}
How would this work with something more complex?
This does not work
#[derive(Deserialize, Debug, Clone)]
pub struct HttpConfig {
#[serde(with = "serde_yaml::with::singleton_map", default)]
pub middlewares: Option<HashMap<String, Enum>>,
}
It would be awesome if this (imo weird) yaml feature could be turned on/of in the crates feature flags
thank you and all the best
I just found out how it works:
#[derive(Deserialize, Debug, Clone)]
pub struct HttpConfig {
#[serde(with = "serde_yaml::with::singleton_map_recursive")]
pub middlewares: Option<HashMap<String, Enum>>,
}
This doesn't appear to work if you apply it to a struct with an
Option<Enum>
; thewith
attribute somehow wants to treat this as a non-optional and fails the parsing when theOption<Enum>
value is not included in theyaml
file. Perhaps I'm missing something?Originally posted by @LGUG2Z in https://github.com/dtolnay/serde-yaml/issues/298#issuecomment-1209928426
I have included a minimal reproducible example demonstrating this case:
This gives the following error: