Dont Error on Invalid Enum Values (#5169)
* Dont Error on Invalid Enum * Use TryParse * Log warning
This commit is contained in:
parent
8954ff3af2
commit
306f7e93a0
1 changed files with 7 additions and 4 deletions
|
@ -1,4 +1,5 @@
|
||||||
#nullable enable
|
#nullable enable
|
||||||
|
using Ryujinx.Common.Logging;
|
||||||
using System;
|
using System;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
|
@ -18,12 +19,14 @@ namespace Ryujinx.Common.Utilities
|
||||||
public override TEnum Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
public override TEnum Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||||
{
|
{
|
||||||
var enumValue = reader.GetString();
|
var enumValue = reader.GetString();
|
||||||
if (string.IsNullOrEmpty(enumValue))
|
|
||||||
|
if (Enum.TryParse(enumValue, out TEnum value))
|
||||||
{
|
{
|
||||||
return default;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Enum.Parse<TEnum>(enumValue);
|
Logger.Warning?.Print(LogClass.Configuration, $"Failed to parse enum value \"{enumValue}\" for {typeof(TEnum)}, using default \"{default(TEnum)}\"");
|
||||||
|
return default;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Write(Utf8JsonWriter writer, TEnum value, JsonSerializerOptions options)
|
public override void Write(Utf8JsonWriter writer, TEnum value, JsonSerializerOptions options)
|
||||||
|
@ -31,4 +34,4 @@ namespace Ryujinx.Common.Utilities
|
||||||
writer.WriteStringValue(value.ToString());
|
writer.WriteStringValue(value.ToString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in a new issue