mirror of
https://github.com/LemmyNet/activitypub-federation-rust
synced 2024-11-23 20:03:14 +00:00
Fix deserialize_skip_error function (#24)
https://github.com/serde-rs/serde/issues/1726#issuecomment-577425541
This commit is contained in:
parent
463580d734
commit
6d9682f4e6
1 changed files with 22 additions and 5 deletions
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue