Created
March 22, 2019 06:24
-
-
Save PawelGerr/885a34783e46619dcd0c6ebb028908c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EnumJsonConverter<TEnum, T> : JsonConverter<Enum<TEnum, T>> | |
where TEnum : Enum<TEnum, T> | |
{ | |
public override void WriteJson(JsonWriter writer, Enum<TEnum, T> value, JsonSerializer serializer) | |
{ | |
if (value == null) | |
{ | |
writer.WriteNull(); | |
} | |
else | |
{ | |
serializer.Serialize(writer, value.Key); | |
} | |
} | |
public override Enum<TEnum, T> ReadJson(JsonReader reader, Type objectType, Enum<TEnum, T> existingValue, bool hasExistingValue, JsonSerializer serializer) | |
{ | |
if (reader.TokenType == JsonToken.Null) | |
return null; | |
var key = serializer.Deserialize<T>(reader); | |
return Enum<TEnum, T>.Get(key); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment