How to define structs for mixed array when deserializing in serde?

The JSON array mixes string and object like

{
   "text": [
    {
     "type": "mention_name",
     "text": "Uopju Mnohu🎀 ",
     "user_id": 5149962590
    },
    " has been banned! Reason: ",
    {
     "type": "text_link",
     "text": "CAS ban",
     "href": "https://cas.chat/query?u=5149962590"
    },
    "."
   ]
}

I tried something like but it does not work.

#[derive(Debug, Clone, Serialize, Deserialize)]
struct Text {
    text: String,
    r#type: String,
    user_id: Option<i64>,
    href: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
enum TextItem {   // ???
    String(String), 
    Text(Text),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct Message {
    text: Vec<TextItem>
}

Thanks.

Seems #[serde(untagged)] is all I need.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.