1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-26 14:38:34 +01:00

added dynamic enum json converter

This commit is contained in:
Ell 2021-06-08 14:45:46 +02:00
parent e0a9971bdb
commit b0d146849d
3 changed files with 23 additions and 1 deletions

View file

@ -3,7 +3,9 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Numerics; using System.Numerics;
using System.Reflection; using System.Reflection;
using MLEM.Data.Json;
using MLEM.Misc; using MLEM.Misc;
using Newtonsoft.Json;
namespace MLEM.Data { namespace MLEM.Data {
/// <summary> /// <summary>
@ -23,6 +25,7 @@ namespace MLEM.Data {
/// public static MyEnum operator ~(MyEnum value) => Neg(value); /// public static MyEnum operator ~(MyEnum value) => Neg(value);
/// </code> /// </code>
/// </remarks> /// </remarks>
[JsonConverter(typeof(DynamicEnumConverter))]
public abstract class DynamicEnum : GenericDataHolder { public abstract class DynamicEnum : GenericDataHolder {
private static readonly Dictionary<Type, Dictionary<BigInteger, DynamicEnum>> Values = new Dictionary<Type, Dictionary<BigInteger, DynamicEnum>>(); private static readonly Dictionary<Type, Dictionary<BigInteger, DynamicEnum>> Values = new Dictionary<Type, Dictionary<BigInteger, DynamicEnum>>();

View file

@ -0,0 +1,19 @@
using System;
using Newtonsoft.Json;
namespace MLEM.Data.Json {
/// <inheritdoc />
public class DynamicEnumConverter : JsonConverter<DynamicEnum> {
/// <inheritdoc />
public override void WriteJson(JsonWriter writer, DynamicEnum value, JsonSerializer serializer) {
writer.WriteValue(value.ToString());
}
/// <inheritdoc />
public override DynamicEnum ReadJson(JsonReader reader, Type objectType, DynamicEnum existingValue, bool hasExistingValue, JsonSerializer serializer) {
return DynamicEnum.Parse(objectType, reader.Value.ToString());
}
}
}

View file

@ -30,6 +30,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="../Media/Logo.png" Pack="true" PackagePath=""/> <None Include="../Media/Logo.png" Pack="true" PackagePath="" />
</ItemGroup> </ItemGroup>
</Project> </Project>