Skip to content

Instantly share code, notes, and snippets.

@aletheia
Last active September 7, 2022 20:31
Show Gist options
  • Save aletheia/90087978f6f3f9c2adcc3047349c7cab to your computer and use it in GitHub Desktop.
Save aletheia/90087978f6f3f9c2adcc3047349c7cab to your computer and use it in GitHub Desktop.
#[derive(Deserialize, Serialize)]
struct User {
customer_id: String,
senior_citizen: i32,
#[serde(default, rename = "churnProbability")]
churn_probability: Option<f64>,
#[serde(rename = "MonthlyCharges")]
monthly_charges: f64,
#[serde(rename = "TotalCharges")]
total_charges: f64,
is_male: i32,
is_partner: i32,
payment_method: i32,
// [...] other attributes
}
// Usage example. Let's say we've a serialized JSON in a serialized_user variable, then deserialization is as simple as
let mut user: User = serde_json::from_str(&serialized_user)?;
@shinglyu
Copy link

Maybe a usage example would be helpful.
Also we can omit some of the i32 and f64 fields that doesn't have special usage.

@aletheia
Copy link
Author

aletheia commented Sep 7, 2022

done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment