3
votes

I'm trying to convert a JSON object to a C# class using .NET Core (5) System.Text.Json JsonSerializer.Deserialize() but it's throwing a JsonException. I believe the reason is because the string values it's trying to convert to an enum contains dashes (hyphens). The enum looks like this:

    [JsonConverter(typeof(JsonStringEnumConverter))]
    public enum TypeEnum
    {
        [EnumMember(Value = "loopback")]
        Loopback = 1,

        [EnumMember(Value = "link-local")]
        LinkLocal = 2,
    }

The JSON object is quite large but the part that is causing the exception is this:

{
    "address": "fe80::3617:ebff:fec0:b54c/64"
    "family": "inet6",
    "type": "link-local"
}

When the JsonSerializer encounters "type":"link-local", it doesn't like "link-local" and throws. Is there any way to fix this?

Thanks.

Was asked for the exception so here it is:

 System.Text.Json.JsonException : The JSON value could not be converted to DmIfInfoAddr+TypeEnum. Path: $[0].addrs[0].type | LineNumber: 0 | BytePositionInLine: 144.
  Stack Trace: 
    ThrowHelper.ThrowJsonException(String message)
    EnumConverter`1.ReadWithQuotes(Utf8JsonReader& reader)
    EnumConverter`1.Read(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options)
    JsonPropertyInfo`1.ReadJsonAndSetMember(Object obj, ReadStack& state, Utf8JsonReader& reader)
    ObjectDefaultConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
    JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
    IEnumerableDefaultConverter`2.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, TCollection& value)
    JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
    JsonPropertyInfo`1.ReadJsonAndSetMember(Object obj, ReadStack& state, Utf8JsonReader& reader)
    ObjectDefaultConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
    JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
    IEnumerableDefaultConverter`2.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, TCollection& value)
    JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
    JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
    JsonSerializer.ReadCore[TValue](JsonConverter jsonConverter, Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
    JsonSerializer.ReadCore[TValue](Utf8JsonReader& reader, Type returnType, JsonSerializerOptions options)
    JsonSerializer.Deserialize[TValue](String json, Type returnType, JsonSerializerOptions options)
    JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
1
What's the exception?CodingGorilla
I added it to the post.DaveR
After a cursory review of the EnumConverter in the runtime repo, it doesn't appear that it respects the EnumMemberAttribute (which isn't technically part of System.Text.Json). I would recommend writing a custom coverter.CodingGorilla

1 Answers

3
votes

As of now there is no support by System.Text.Json for System.Runtime.Serialization attributes as you can see in the migration from Newtonsoft.Json guide. There is an issue on github which you can track. As workaround you can try to use JsonStringEnumMemberConverter from Macross.Json.Extensions.