Fix deserialize_skip_error function (#24)

https://github.com/serde-rs/serde/issues/1726#issuecomment-577425541
This commit is contained in:
Nutomic 2023-02-05 01:07:13 +09:00 committed by GitHub
parent 463580d734
commit 6d9682f4e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -58,9 +58,26 @@ where
T: Deserialize<'de> + Default,
D: Deserializer<'de>,
{
let result = Deserialize::deserialize(deserializer);
Ok(match result {
Ok(o) => o,
Err(_) => Default::default(),
})
let value = serde_json::Value::deserialize(deserializer)?;
let inner = T::deserialize(value).unwrap_or_default();
Ok(inner)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_deserialize_skip_error() {
#[derive(Debug, Deserialize)]
pub struct MyData {
#[serde(deserialize_with = "deserialize_skip_error")]
pub data: Option<String>,
}
// data has type object
let _: MyData = serde_json::from_str(r#"{ "data": {} }"#).unwrap();
// data has type array
let _: MyData = serde_json::from_str(r#"{"data": []}"#).unwrap();
}
}