From c0efbb22ccf26b7ce8b26321c67f30719edaa7f7 Mon Sep 17 00:00:00 2001 From: glax Date: Thu, 31 Aug 2023 15:41:02 +0200 Subject: [PATCH] Fixed JsonParsing of NotifcationConnector and LibraryConnector with GlobalBase --- Tranga/GlobalBase.cs | 4 ++-- .../LibraryManagerJsonConverter.cs | 19 +++++++++++++++---- .../NotificationManagerJsonConverter.cs | 14 +++++++++++--- Tranga/TrangaSettings.cs | 10 +++++----- 4 files changed, 33 insertions(+), 14 deletions(-) diff --git a/Tranga/GlobalBase.cs b/Tranga/GlobalBase.cs index 8cad5c6..063054e 100644 --- a/Tranga/GlobalBase.cs +++ b/Tranga/GlobalBase.cs @@ -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(); } diff --git a/Tranga/LibraryConnectors/LibraryManagerJsonConverter.cs b/Tranga/LibraryConnectors/LibraryManagerJsonConverter.cs index f8a4d11..be8983f 100644 --- a/Tranga/LibraryConnectors/LibraryManagerJsonConverter.cs +++ b/Tranga/LibraryConnectors/LibraryManagerJsonConverter.cs @@ -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)LibraryConnector.LibraryType.Komga) - return jo.ToObject(serializer)!; + if (jo["libraryType"]!.Value() == (byte)LibraryConnector.LibraryType.Komga) + return new Komga(this._clone, + jo.GetValue("baseUrl")!.Value()!, + jo.GetValue("auth")!.Value()!); - if (jo["libraryType"]!.Value() == (Int64)LibraryConnector.LibraryType.Kavita) - return jo.ToObject(serializer)!; + if (jo["libraryType"]!.Value() == (byte)LibraryConnector.LibraryType.Kavita) + return new Kavita(this._clone, + jo.GetValue("baseUrl")!.Value()!, + jo.GetValue("auth")!.Value()!); throw new Exception(); } diff --git a/Tranga/NotificationConnectors/NotificationManagerJsonConverter.cs b/Tranga/NotificationConnectors/NotificationManagerJsonConverter.cs index a85a226..2b2d843 100644 --- a/Tranga/NotificationConnectors/NotificationManagerJsonConverter.cs +++ b/Tranga/NotificationConnectors/NotificationManagerJsonConverter.cs @@ -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)NotificationConnector.NotificationManagerType.Gotify) - return jo.ToObject(serializer)!; - else if (jo["notificationManagerType"]!.Value() == (byte)NotificationConnector.NotificationManagerType.LunaSea) - return jo.ToObject(serializer)!; + return new Gotify(this._clone, jo.GetValue("endpoint")!.Value()!, jo.GetValue("appToken")!.Value()!); + else if (jo["notificationManagerType"]!.Value() == + (byte)NotificationConnector.NotificationManagerType.LunaSea) + return new LunaSea(this._clone, jo.GetValue("id")!.Value()!); throw new Exception(); } diff --git a/Tranga/TrangaSettings.cs b/Tranga/TrangaSettings.cs index dcfe66d..4808a04 100644 --- a/Tranga/TrangaSettings.cs +++ b/Tranga/TrangaSettings.cs @@ -50,7 +50,7 @@ public class TrangaSettings UpdateDownloadLocation(this.downloadLocation!, false); } - public HashSet LoadLibraryConnectors() + public HashSet LoadLibraryConnectors(GlobalBase clone) { if (!File.Exists(libraryConnectorsFilePath)) return new HashSet(); @@ -59,21 +59,21 @@ public class TrangaSettings { Converters = { - new LibraryManagerJsonConverter() + new LibraryManagerJsonConverter(clone) } })!; } - public HashSet LoadNotificationConnectors() + public HashSet LoadNotificationConnectors(GlobalBase clone) { if (!File.Exists(notificationConnectorsFilePath)) return new HashSet(); - return JsonConvert.DeserializeObject>(File.ReadAllText(libraryConnectorsFilePath), + return JsonConvert.DeserializeObject>(File.ReadAllText(notificationConnectorsFilePath), new JsonSerializerSettings() { Converters = { - new NotificationManagerJsonConverter() + new NotificationManagerJsonConverter(clone) } })!; }