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)
EnumConverter
in the runtime repo, it doesn't appear that it respects theEnumMemberAttribute
(which isn't technically part of System.Text.Json). I would recommend writing a custom coverter. – CodingGorilla