#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()); } } }