CShock/CShocker/Shockers/Additional/ShockerJsonConverter.cs

36 lines
1000 B
C#
Raw Normal View History

2024-02-04 17:47:36 +01:00
using CShocker.Shockers.Abstract;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace CShocker.Shockers.Additional;
public class ShockerJsonConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(Shocker));
}
public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{
JObject jo = JObject.Load(reader);
2024-02-15 23:11:56 +01:00
if (jo.ContainsKey("Model")) //OpenShockShocker
{
2024-02-04 17:47:36 +01:00
return jo.ToObject<OpenShockShocker>(serializer)!;
}
else //PiShockShocker
{
2024-02-04 17:47:36 +01:00
return jo.ToObject<PiShockShocker>(serializer)!;
}
}
public override bool CanWrite => false;
/// <summary>
/// Don't call this
/// </summary>
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
2024-02-12 02:01:31 +01:00
throw new NotImplementedException("Dont call this");
}
}