Serde_json untagged enum
⚓ Rust 📅 2025-05-06 👤 surdeus 👁️ 10I'm trying to deserialize JSON structures like below while making the best possible use of enums:
{
"id": "1",
"schema": "Person",
"properties": {
"name": "John Doe"
}
}
but also
{
"id": "4",
"schema": "Company",
"properties": {
"employee_count": 42,
}
}
Is it at all possible to have something like this to deserialize into? Or do I need a custom deserializer?
#[derive(Debug, Deserialize)]
#[serde(tag = "schema", content = "properties")]
enum EntityType {
Person {
name: String,
},
Company {
employee_count: usize,
},
}
2 posts - 2 participants
🏷️ rust_feed