2021-06-11 20:05:32 +02:00
|
|
|
using System;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
|
|
namespace MLEM.Data.Json {
|
2021-11-22 19:25:18 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Converts a <see cref="DynamicEnum"/> to and from JSON
|
|
|
|
/// </summary>
|
2021-06-11 20:05:32 +02:00
|
|
|
public class DynamicEnumConverter : JsonConverter<DynamicEnum> {
|
|
|
|
|
2021-11-22 19:25:18 +01:00
|
|
|
/// <summary>Writes the JSON representation of the object.</summary>
|
|
|
|
/// <param name="writer">The <see cref="T:Newtonsoft.Json.JsonWriter" /> to write to.</param>
|
|
|
|
/// <param name="value">The value.</param>
|
|
|
|
/// <param name="serializer">The calling serializer.</param>
|
2021-06-11 20:05:32 +02:00
|
|
|
public override void WriteJson(JsonWriter writer, DynamicEnum value, JsonSerializer serializer) {
|
|
|
|
writer.WriteValue(value.ToString());
|
|
|
|
}
|
|
|
|
|
2021-11-22 19:25:18 +01:00
|
|
|
/// <summary>Reads the JSON representation of the object.</summary>
|
|
|
|
/// <param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader" /> to read from.</param>
|
|
|
|
/// <param name="objectType">Type of the object.</param>
|
|
|
|
/// <param name="existingValue">The existing value of object being read. If there is no existing value then <c>null</c> will be used.</param>
|
|
|
|
/// <param name="hasExistingValue">The existing value has a value.</param>
|
|
|
|
/// <param name="serializer">The calling serializer.</param>
|
|
|
|
/// <returns>The object value.</returns>
|
2021-06-11 20:05:32 +02:00
|
|
|
public override DynamicEnum ReadJson(JsonReader reader, Type objectType, DynamicEnum existingValue, bool hasExistingValue, JsonSerializer serializer) {
|
|
|
|
return DynamicEnum.Parse(objectType, reader.Value.ToString());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|