Fixed JsonParsing of NotifcationConnector and LibraryConnector with GlobalBase

This commit is contained in:
glax 2023-08-31 15:41:02 +02:00
parent 9f30e52713
commit c0efbb22cc
4 changed files with 33 additions and 14 deletions

View File

@ -26,8 +26,8 @@ public abstract class GlobalBase
{
this.logger = logger;
this.settings = settings;
this.notificationConnectors = settings.LoadNotificationConnectors();
this.libraryConnectors = settings.LoadLibraryConnectors();
this.notificationConnectors = settings.LoadNotificationConnectors(this);
this.libraryConnectors = settings.LoadLibraryConnectors(this);
this.cachedPublications = new();
}

View File

@ -5,6 +5,13 @@ namespace Tranga.LibraryConnectors;
public class LibraryManagerJsonConverter : JsonConverter
{
private GlobalBase _clone;
internal LibraryManagerJsonConverter(GlobalBase clone)
{
this._clone = clone;
}
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(LibraryConnector));
@ -13,11 +20,15 @@ public class LibraryManagerJsonConverter : JsonConverter
public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{
JObject jo = JObject.Load(reader);
if (jo["libraryType"]!.Value<Int64>() == (Int64)LibraryConnector.LibraryType.Komga)
return jo.ToObject<Komga>(serializer)!;
if (jo["libraryType"]!.Value<byte>() == (byte)LibraryConnector.LibraryType.Komga)
return new Komga(this._clone,
jo.GetValue("baseUrl")!.Value<string>()!,
jo.GetValue("auth")!.Value<string>()!);
if (jo["libraryType"]!.Value<Int64>() == (Int64)LibraryConnector.LibraryType.Kavita)
return jo.ToObject<Kavita>(serializer)!;
if (jo["libraryType"]!.Value<byte>() == (byte)LibraryConnector.LibraryType.Kavita)
return new Kavita(this._clone,
jo.GetValue("baseUrl")!.Value<string>()!,
jo.GetValue("auth")!.Value<string>()!);
throw new Exception();
}

View File

@ -5,6 +5,13 @@ namespace Tranga.NotificationConnectors;
public class NotificationManagerJsonConverter : JsonConverter
{
private GlobalBase _clone;
public NotificationManagerJsonConverter(GlobalBase clone)
{
this._clone = clone;
}
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(NotificationConnector));
@ -15,9 +22,10 @@ public class NotificationManagerJsonConverter : JsonConverter
{
JObject jo = JObject.Load(reader);
if (jo["notificationManagerType"]!.Value<byte>() == (byte)NotificationConnector.NotificationManagerType.Gotify)
return jo.ToObject<Gotify>(serializer)!;
else if (jo["notificationManagerType"]!.Value<byte>() == (byte)NotificationConnector.NotificationManagerType.LunaSea)
return jo.ToObject<LunaSea>(serializer)!;
return new Gotify(this._clone, jo.GetValue("endpoint")!.Value<string>()!, jo.GetValue("appToken")!.Value<string>()!);
else if (jo["notificationManagerType"]!.Value<byte>() ==
(byte)NotificationConnector.NotificationManagerType.LunaSea)
return new LunaSea(this._clone, jo.GetValue("id")!.Value<string>()!);
throw new Exception();
}

View File

@ -50,7 +50,7 @@ public class TrangaSettings
UpdateDownloadLocation(this.downloadLocation!, false);
}
public HashSet<LibraryConnector> LoadLibraryConnectors()
public HashSet<LibraryConnector> LoadLibraryConnectors(GlobalBase clone)
{
if (!File.Exists(libraryConnectorsFilePath))
return new HashSet<LibraryConnector>();
@ -59,21 +59,21 @@ public class TrangaSettings
{
Converters =
{
new LibraryManagerJsonConverter()
new LibraryManagerJsonConverter(clone)
}
})!;
}
public HashSet<NotificationConnector> LoadNotificationConnectors()
public HashSet<NotificationConnector> LoadNotificationConnectors(GlobalBase clone)
{
if (!File.Exists(notificationConnectorsFilePath))
return new HashSet<NotificationConnector>();
return JsonConvert.DeserializeObject<HashSet<NotificationConnector>>(File.ReadAllText(libraryConnectorsFilePath),
return JsonConvert.DeserializeObject<HashSet<NotificationConnector>>(File.ReadAllText(notificationConnectorsFilePath),
new JsonSerializerSettings()
{
Converters =
{
new NotificationManagerJsonConverter()
new NotificationManagerJsonConverter(clone)
}
})!;
}