C#C
C#3y ago
27 replies
Playwo

Parse Rust Enum Json in C#

Rust enums are a cool feature for Rust devs, but now that I gotta parse them in C# I hate them.

I got a JSON input that looks like this:
{
    "Key": "DoesNotExist",
    "Key2": {
        "Position": 10
    },
    "Key3": {
        "Order": {
            "Id": 10,
            "Name": "123"
        }
    }
}


Key1, Key2, Key3 are 3 possible Enum values in Rust. But I cannot seem to find any reasonable way to parse this in C#.

Yes I could write a JsonConverter for this simple case, in practice I am working on a source generator that has to work on any input where often times enums like this are nested in each other and having to create a JsonConverter for that entire tree is just pain :/
Was this page helpful?