I've posted this on https://github.com/serde-rs/serde/issues/970#issuecomment-438153519
I'm re-posting here because I've gone through the docs and haven't found a solution that I can grok.
I've looked:
- serde_with::with_prefix - Rust
- https://github.com/serde-rs/serde/issues/595
- Manually deserialize struct · Serde - The fact that the field names need to be known beforehand seems to preclude this. I'm hoping that there's something I'm missing.
I'm stuck using a 3rd party API that uses a convention in the same vein, but doesn't fit the serde_with use case; or perhaps I'm not smart enough to see how it fits.
I have a response with the following structure:
{
"something": [
{
"name": "foo",
"annoying": [
{ "name": "thing1", "foo_cow": 1, "foo_dog": 2, "foo_cat": 3 },
{ "name": "thing2", "foo_cow": 2, "foo_dog": 3, "foo_cat": 4},
]
},
{
"name": "bar",
"annoying": [
{ "name": "thing1", "bar_cow": 1, "bar_dog": 2, "bar_cat": 3 },
{ "name": "thing2", "bar_cow": 2, "bar_dog": 3, "bar_cat": 4},
]
},
{
"name": "baz",
"annoying": [
{ "name": "thing1", "baz_cow": 1, "baz_dog": 2, "baz_cat": 3 },
{ "name": "thing2", "baz_cow": 2, "baz_dog": 3, "baz_cat": 4},
]
}]
}
I want to deserialize into:
#[derive(Deserialize)]
pub struct Member {
name: String,
cow: u16,
dog: u16,
cat: u16,
}
#[derive(Deserialize)]
pub struct Group {
name: String,
#[serde(rename = "annoying")]
members: Vec<Member>
}
#[derive(Deserialize)]
pub struct Collection {
#[serde(rename = "something")]
items: Vec<Group>
}
Is there a macro to deserialize based on a regex field match? Any help would be sanity saving