The following code snippet is how I use serde::Deserialize;
#[derive(Debug, Deserialize)]
struct OuterStruct{
name: String,
value: i64,
inner: Wrapper,
}
#[derive(Debug, Deserialize)]
struct structA{
#[serde(rename = "type")]
ttype: String
field: String
}
#[derive(Debug, Deserialize)]
struct structB{
#[serde(rename = "type")]
ttype: String
field: String
}
#[derive(Debug, Deserialize)]
#[serde(tag = "type")]
enum Wrapper{
typea(structA),
typeb(structB),
}
To my understanding the #[serde(tag = "type")]
specifies the field type which decides what enum's variant to use for Deserialize
.
But when I use it like that I get errors like
missing field ``type`` at line 1 column 177
I've also tried #[serde(tag = "ttype")] which is the original field name but to no avail.
My current workaround is:
Like cafce25 mentioned in the answer
removing ttype
from the struct.
And also do
impl Wrapper{
fn get_type(&self)->String{
match &self{
Wrapper::typea(_)=>"typea".to_string(),
Wrapper::typeb(_)=>"typeb".to_string(),
}
}
}
Is there anyway for me to keep type
field within the struct?