Skip to content

Commit 0b85239

Browse files
Create EnumCache json converter
1 parent 70678db commit 0b85239

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/Json/EnumCacheJson.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Text.Json;
2+
using System.Text.Json.Serialization;
3+
4+
namespace PolyMod.Json
5+
{
6+
internal class EnumCacheJson<T> : JsonConverter<T> where T : struct, Enum
7+
{
8+
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
9+
{
10+
return EnumCache<T>.GetType(reader.GetString());
11+
}
12+
13+
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
14+
{
15+
writer.WriteStringValue(EnumCache<T>.GetName(value));
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)