#Avoiding a clone() in a custom JSON deserializer

4 messages · Page 1 of 1 (latest)

copper spire
#

I have the following custom JSON deserializer to deserialize an object only as its value property:

use serde::{de, Deserialize, Deserializer};
use serde_json::Value;

fn property_value<'de, T, D>(deserializer: D) -> Result<T, D::Error>      
where      
    T: Deserialize<'de>,      
    D: Deserializer<'de>,      
{      
    let property = Value::deserialize(deserializer)?;   
    match property {   
        Value::Object(map) => {      
            let v = map      
                .get("value")      
                .ok_or_else(|| de::Error::missing_field("value"))?       
                .clone();
            T::deserialize(v).map_err(|_| de::Error::custom("unexpected value type"))            
        }
        _ => Err(de::Error::custom("expected an object")),      
    }
}   

Is there a way to avoid the clone()?

vast ivy
#

from what I can see, you should be able to remove the value from the map as the map is being dropped anyway.

copper spire
daring hedge