mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-25 22:18:34 +01:00
added dynamic enum json converter
This commit is contained in:
parent
e0a9971bdb
commit
b0d146849d
3 changed files with 23 additions and 1 deletions
|
@ -3,7 +3,9 @@ using System.Collections.Generic;
|
|||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Reflection;
|
||||
using MLEM.Data.Json;
|
||||
using MLEM.Misc;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace MLEM.Data {
|
||||
/// <summary>
|
||||
|
@ -23,6 +25,7 @@ namespace MLEM.Data {
|
|||
/// public static MyEnum operator ~(MyEnum value) => Neg(value);
|
||||
/// </code>
|
||||
/// </remarks>
|
||||
[JsonConverter(typeof(DynamicEnumConverter))]
|
||||
public abstract class DynamicEnum : GenericDataHolder {
|
||||
|
||||
private static readonly Dictionary<Type, Dictionary<BigInteger, DynamicEnum>> Values = new Dictionary<Type, Dictionary<BigInteger, DynamicEnum>>();
|
||||
|
|
19
MLEM.Data/Json/DynamicEnumConverter.cs
Normal file
19
MLEM.Data/Json/DynamicEnumConverter.cs
Normal 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());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue