Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Newtonsoft - JsonConverterVersion2
// Import Newtonsoft.Json
public class EnumerationJsonConverter : JsonConverter<Enumeration>
{
public override void WriteJson(JsonWriter writer, Enumeration value, JsonSerializer serializer)
{
if (value is null)
{
writer.WriteNull();
}
else
{
writer.WriteValue(value.Name);
}
}
public override Enumeration ReadJson(JsonReader reader,
Type objectType,
Enumeration existingValue,
bool hasExistingValue,
JsonSerializer serializer)
{
return reader.TokenType switch
{
JsonToken.Integer => GetEnumerationFromJson(reader.Value.ToString(), objectType),
JsonToken.String => GetEnumerationFromJson(reader.Value.ToString(), objectType),
JsonToken.Null => null,
_ => throw new JsonSerializationException($"Unexpected token {reader.TokenType} when parsing an enumeration")
};
}
private static Enumeration GetEnumerationFromJson(string nameOrValue, Type objectType)
{
try
{
object result = default;
var methodInfo = typeof(Enumeration).GetMethod(
nameof(Enumeration.TryGetFromValueOrName)
, BindingFlags.Static | BindingFlags.Public);
if (methodInfo == null)
{
throw new JsonSerializationException("Serialization is not supported");
}
var genericMethod = methodInfo.MakeGenericMethod(objectType);
var arguments = new[] { nameOrValue, result };
genericMethod.Invoke(null, arguments);
return arguments[1] as Enumeration;
}
catch (Exception ex)
{
throw new JsonSerializationException($"Error converting value '{nameOrValue}' to a enumeration.", ex);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.