Created
January 23, 2024 01:15
-
-
Save DefectingCat/782c3558a3a5d6b6d871bc758c5513e6 to your computer and use it in GitHub Desktop.
deserialize with custom function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn de_str<'de, D>(deserializer: D) -> Result<i64, D::Error> | |
where | |
D: serde::de::Deserializer<'de>, | |
{ | |
Ok(match Value::deserialize(deserializer)? { | |
Value::String(s) => s.parse().map_err(de::Error::custom)?, | |
Value::Number(num) => num.as_i64().ok_or(de::Error::custom("Invalide number"))?, | |
_ => return Err(de::Error::custom("wrong type")), | |
}) | |
} | |
#[derive(Serialize, Deserialize, Debug)] | |
pub struct BoothExamplesList { | |
pub total: i64, | |
#[serde(deserialize_with = "de_str")] | |
pub per_page: i64, | |
pub current_page: i64, | |
pub last_page: i64, | |
pub data: Vec<BoothExamplesDatum>, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment