0

Id like to divide my_val by 100 on deserialization:

#[derive(Debug, Deserialize, Clone)]
pub struct MyObject {
    pub other: String,
    pub my_val: f64, // <-- divide by 100.0
}

I found deserialize_with but the only examples I could find is conversion from string into float

#[serde(deserialize_with = "my_val_division")]
pub my_val: f64,

and

fn my_val_division<'de, D>(deserializer: D) -> Result<u64, D::Error>
where
    D: Deserializer<'de>,
{
    // what to do here?
}

How to do the division on deserialization?

PascalVKooten
  • 20,643
  • 17
  • 103
  • 160

1 Answers1

1

Eventually I found it:

use serde::{Deserialize, Deserializer};

fn my_val_division<'de, D>(deserializer: D) -> Result<f64, D::Error>
where
    D: de::Deserializer<'de>,
{
    let vw: f64 = serde::de::Deserialize::deserialize(deserializer)?;
    Ok(vw / 100.0)
}
PascalVKooten
  • 20,643
  • 17
  • 103
  • 160