Skip to content

Commit

Permalink
fix game info json converter
Browse files Browse the repository at this point in the history
  • Loading branch information
Scighost committed Jun 1, 2023
1 parent fa71994 commit ea1ba25
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions Starward.Core/Metadata/GameInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json.Serialization;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Starward.Core.Metadata;

Expand All @@ -9,7 +10,7 @@ public class GameInfo
public string Name { get; set; }


[JsonConverter(typeof(JsonStringEnumConverter))]
[JsonConverter(typeof(EnumStringJsonConverter<GameBiz>))]
public GameBiz GameBiz { get; set; }


Expand All @@ -29,3 +30,25 @@ public class GameInfo


}



internal class EnumStringJsonConverter<T> : JsonConverter<T> where T : struct
{
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (Enum.TryParse(reader.GetString(), out T result))
{
return result;
}
else
{
return default;
}
}

public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
}

0 comments on commit ea1ba25

Please sign in to comment.