Skip to content

Instantly share code, notes, and snippets.

@mikechambers
Created November 14, 2020 23:06
Show Gist options
  • Save mikechambers/e00256003a48aa9f173f9b013e4fcc88 to your computer and use it in GitHub Desktop.
Save mikechambers/e00256003a48aa9f173f9b013e4fcc88 to your computer and use it in GitHub Desktop.
Custom JSON property Deserialization in Rust with Serde
#[derive(Serialize, Deserialize, Debug)]
struct ManifestInfo {
version:String,
#[serde(rename(serialize = "url", deserialize = "mobileWorldContentPaths"), alias="url", deserialize_with = "deserialize_mobile_world_content_Paths")]
url:String,
}
fn deserialize_mobile_world_content_Paths<'de, D>(deserializer: D) -> Result<String, D::Error> where D: serde::de::Deserializer<'de>
{
#[derive(Deserialize)]
#[serde (rename="mobileWorldContentPaths")]
struct MobileWorldContentPaths {
en: String,
}
//TODO: move to URL base to constant
MobileWorldContentPaths::deserialize(deserializer).map(|mobileWorldContentPaths| format!("https://www.bungie.net{}",mobileWorldContentPaths.en))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment