From cee712105850ac3385cd0091a923438167433f9f Mon Sep 17 00:00:00 2001 From: TSR Berry <20988865+TSRBerry@users.noreply.github.com> Date: Sat, 8 Apr 2023 01:22:00 +0200 Subject: Move solution and projects to src --- .../Utilities/TypedStringEnumConverter.cs | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/Ryujinx.Common/Utilities/TypedStringEnumConverter.cs (limited to 'src/Ryujinx.Common/Utilities/TypedStringEnumConverter.cs') diff --git a/src/Ryujinx.Common/Utilities/TypedStringEnumConverter.cs b/src/Ryujinx.Common/Utilities/TypedStringEnumConverter.cs new file mode 100644 index 00000000..c0127dc4 --- /dev/null +++ b/src/Ryujinx.Common/Utilities/TypedStringEnumConverter.cs @@ -0,0 +1,34 @@ +#nullable enable +using System; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Ryujinx.Common.Utilities +{ + /// + /// Specifies that value of will be serialized as string in JSONs + /// + /// + /// Trimming friendly alternative to . + /// Get rid of this converter if dotnet supports similar functionality out of the box. + /// + /// Type of enum to serialize + public sealed class TypedStringEnumConverter : JsonConverter where TEnum : struct, Enum + { + public override TEnum Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + var enumValue = reader.GetString(); + if (string.IsNullOrEmpty(enumValue)) + { + return default; + } + + return Enum.Parse(enumValue); + } + + public override void Write(Utf8JsonWriter writer, TEnum value, JsonSerializerOptions options) + { + writer.WriteStringValue(value.ToString()); + } + } +} -- cgit v1.2.3