pub fn patch<'de, D, T>(deserializer: D) -> Result<Option<Option<T>>, D::Error>Expand description
Deserializes a field of a partial update (PATCH) into keep / clear / set.
Use on Option<Option<T>> fields with
#[serde(default, deserialize_with = "ocre::patch")]: a missing field is
None (keep the value, thanks to default), null or an empty string is
Some(None) (clear it), a value is Some(Some(value)). Strings are parsed
with T::from_str, as in optional.
§Errors
Fails with the deserializer’s error, carrying T::Err’s message, when a
non-empty string does not parse.
§Examples
use serde::Deserialize;
#[derive(Deserialize)]
struct BookChanges {
#[serde(default, deserialize_with = "ocre::patch")]
pages: Option<Option<i64>>,
}
let parse = |json| serde_json::from_str::<BookChanges>(json).unwrap().pages;
assert_eq!(parse("{}"), None); // keep
assert_eq!(parse(r#"{"pages": null}"#), Some(None)); // clear
assert_eq!(parse(r#"{"pages": ""}"#), Some(None)); // clear (HTML form)
assert_eq!(parse(r#"{"pages": 12}"#), Some(Some(12))); // set