Serde_json untagged enum

⚓ rust    📅 2025-05-06    👤 surdeus    👁️ 4      

surdeus

Warning

This post was published 56 days ago. The information described in this article may have changed.

Info

This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Serde_json untagged enum

I'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,
        }   
    }

Playground link

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

Read full topic

🏷️ rust_feed