And I’m trying to deserialize it, but I can’t seem to get it right. Isn’t this accurate?
#[derive(Deserialize, Debug)]
#[serde(untagged)]
enum NationResponse {
Nation(Nation),
People(Vec),
}
struct Person {
id : i32,
age : i32,
name : String
}
struct Nation {
id : i32,
country : String
}
Edit:
The problem I was actually experiencing was trying to use an enum as the response. Yes the formatting for my example was wrong (it should have been an array). But the structure besides that was accurate.
So, no. With the way you have it setup right now you would need to adjust your JSON structure to have the nation info be under a key, as well as the people array.
{
"Nation": {...},
"People": [...],
}
Every value has to have a key, unless it is the only value being serialized.
Yeah, I wasn't trying to imply that it was a problem on the rust side or that they needed to name the keys that way, just that the JSON does need to have keys because that is how JSON works
What are you deserializing into? Minimum reproducible Code would be best. That means a main.rs file with a main method and no dependencies, which prints your error when run. Then you can simply share it on the rust playground or ideone.com.
I believe the JSON you have given us is invalid. Either the outer most parentheses must be [] instead of {}, or the nation and the list must get a name, i.e. "nation" = {"id"= 1, "country"="USA"}, "people"= []