How do I deserialize a struct through its own `new` constructor?

Viewed 206

I wish to deserialize the below struct, but using the new constructor to validate the deserialized fields:

#[derive(Deserialize)]
pub struct Timestamp {
    values: Vec<u32>,
}

impl Timestamp {
    pub fn new(values: Vec<u32>) -> Timestamp {
        // some quality check
        Timestamp { values: values }
    }
}

Is there an easy way to do that? I looked into writing a custom Deserializer, but I hoped that there would be an easier and less verbose way to do that.

0 Answers
Related