CShock/CShocker/Devices/Additional/ApiJsonConverter.cs

44 lines
1.3 KiB
C#
Raw Normal View History

using CShocker.Devices.Abstract;
using CShocker.Devices.APIs;
2024-01-17 22:16:40 +01:00
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace CShocker.Devices.Additional;
2024-01-17 22:16:40 +01:00
public class ApiJsonConverter : JsonConverter
2024-01-17 22:16:40 +01:00
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(Api));
2024-01-17 22:16:40 +01:00
}
public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{
JObject jo = JObject.Load(reader);
2024-02-04 17:47:36 +01:00
DeviceApi apiType = (DeviceApi)jo.SelectToken("ApiType")!.Value<byte>();
2024-01-17 22:16:40 +01:00
switch (apiType)
{
case DeviceApi.OpenShockHttp:
2024-02-04 17:47:36 +01:00
return jo.ToObject<OpenShockHttp>()!;
case DeviceApi.OpenShockSerial:
2024-02-04 17:47:36 +01:00
return jo.ToObject<OpenShockSerial>()!;
case DeviceApi.PiShockHttp:
2024-02-04 17:47:36 +01:00
return jo.ToObject<PiShockHttp>()!;
case DeviceApi.PiShockSerial:
return jo.ToObject<PiShockSerial>()!;
2024-01-17 22:16:40 +01:00
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");
}
}