CShock/CShocker/Shockers/ShockerJsonConverter.cs

49 lines
1.6 KiB
C#
Raw Normal View History

2024-01-17 22:16:40 +01:00
using CShocker.Ranges;
using CShocker.Shockers.Abstract;
using CShocker.Shockers.APIS;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace CShocker.Shockers;
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-01-17 22:33:05 +01:00
ShockerApi? apiType = (ShockerApi?)jo.SelectToken("ApiType")?.Value<byte>();
2024-01-17 22:16:40 +01:00
switch (apiType)
{
case ShockerApi.OpenShockHttp:
return new OpenShockHttp(
2024-01-17 22:49:46 +01:00
jo.SelectToken("ShockerIds")!.ToObject<List<string>>()!,
jo.SelectToken("IntensityRange")!.ToObject<IntensityRange>()!,
jo.SelectToken("DurationRange")!.ToObject<DurationRange>()!,
2024-01-17 22:16:40 +01:00
jo.SelectToken("Endpoint")!.Value<string>()!,
jo.SelectToken("ApiKey")!.Value<string>()!
);
case ShockerApi.OpenShockSerial:
case ShockerApi.PiShockHttp:
case ShockerApi.PiShockSerial:
throw new NotImplementedException();
default:
throw new Exception();
}
}
public override bool CanWrite => false;
/// <summary>
/// Don't call this
/// </summary>
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
throw new Exception("Dont call this");
}
}