2

This is a sample JSON object I want to parse:

{  
   "name":"John",
   "balance":"300000",
   "address":"Palo Alto, CA",
   "car":null
}

I've got the following struct using serde_json for parsing JSON:

#[derive(Serialize, Deserialize, Debug)]
pub struct Person {
    #[serde(alias = "name")]
    pub firstname: String,
    #[serde(alias = "balance")]
    pub amount: i64,
    pub address: String,
}

I want the amount field to be i64 instead of String but it's a string in the JSON object.

Is there a field attribute to enforce String -> i64 conversion during parsing? Otherwise, I'll have to create a similar structure where balance is i64 or tune my other project code to accept balance as String which is bad.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
H. Desane
  • 593
  • 1
  • 8
  • 16

0 Answers0