-1

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?

Milan Š.
  • 1,353
  • 1
  • 2
  • 11
Bryant
  • 1
  • 1

1 Answers1

1

The point of #[serde(tag = "type")] is to not have a redundant field on your structs since the information is encoded in the type, just remove the ttype fields from your structs.

use serde::Deserialize;
#[derive(Debug, Deserialize)]
struct StructA {
    field: String,
}
#[derive(Debug, Deserialize)]
struct StructB {
    field: String,
}
#[derive(Debug, Deserialize)]
#[serde(tag = "type")]
enum Wrapper {
    TypeA(StructA),
    TypeB(StructB),
}
cafce25
  • 15,907
  • 4
  • 25
  • 31