Closed mjpauly closed 3 weeks ago
Seems to work fine if the enum field is itself annotated with ok_or_default
.
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct Inner {
pub bool_field: bool,
#[serde(deserialize_with = "ok_or_default")]
pub enum_field: MyEnum,
}
Closing, since this fixes the issue.
I'm using an
ok_or_default
deserializer function (originally from here) to contain deserialization errors with any badly-formed inner fields, replace their values with defaults, and allow well-formed fields and the top-level struct to still deserialize correctly.This works most of the time with JSON input, but there's a case where an error deserializing an inner field can cause the entire deserialization to fail, despite the inner field being deserialized with
ok_or_default
and the input having valid JSON syntax. This seems to happen when:Here is a minimal example, and playground link. The only difference between the
works2
andfails1
cases are the ordering of the fields of theinner
object.Pardon me if this is expected behavior or has been discussed elsewhere already.
Output: