json icon indicating copy to clipboard operation
json copied to clipboard

Implement IntoDeserializer for Value and &Value

Open dtolnay opened this issue 7 years ago • 0 comments

An IntoDeserializer impl would provide a way to deserialize from these types while constructing exactly the right error type on failure, rather than constructing a serde_json::Error and mapping it to a custom error of the target type through its Display impl.

fn f<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where
    T: Deserialize<'de>,
    D: Deserializer<'de>,
{
    let mut v = Value::deserialize(deserializer)?;

    /* some manipulation of v */

    T::deserialize(v).map_err(serde::de::Error::custom)
}
- T::deserialize(v).map_err(serde::de::Error::custom)
+ T::deserialize(v.into_deserializer())

dtolnay avatar Mar 12 '18 18:03 dtolnay