From afe36ab2ef40cc715568d44c7ec3715809c745b1 Mon Sep 17 00:00:00 2001 From: glax Date: Fri, 19 May 2023 16:23:37 +0200 Subject: [PATCH] Switch to Newtonsoft.Json for serialization --- Tranga/Publication.cs | 9 ++++----- Tranga/TaskManager.cs | 20 ++++++++++++-------- Tranga/Tranga.csproj | 4 ++++ Tranga/TrangaTask.cs | 16 +++++++--------- 4 files changed, 27 insertions(+), 22 deletions(-) diff --git a/Tranga/Publication.cs b/Tranga/Publication.cs index 907b327..597f15b 100644 --- a/Tranga/Publication.cs +++ b/Tranga/Publication.cs @@ -1,16 +1,15 @@ -using System.Text.Json; -using System.Text.Json.Serialization; +using Newtonsoft.Json; namespace Tranga; public struct Publication { public string sortName { get; } - public string[,] altTitles { get; } + [JsonIgnore]public string[,] altTitles { get; } public string? description { get; } public string[] tags { get; } public string? posterUrl { get; } - public string[,]? links { get; } + [JsonIgnore]public string[,]? links { get; } public int? year { get; } public string? originalLanguage { get; } public string status { get; } @@ -35,7 +34,7 @@ public struct Publication public string GetSeriesInfo() { SeriesInfo si = new (new Metadata(this.sortName, this.year.ToString() ?? string.Empty, this.status, this.description ?? "")); - return JsonSerializer.Serialize(si, JsonSerializerOptions.Default); + return System.Text.Json.JsonSerializer.Serialize(si); } internal struct SeriesInfo diff --git a/Tranga/TaskManager.cs b/Tranga/TaskManager.cs index 3637c88..e50f991 100644 --- a/Tranga/TaskManager.cs +++ b/Tranga/TaskManager.cs @@ -1,4 +1,5 @@ -using System.Text.Json; +using Newtonsoft.Json; +using Tranga.Connectors; namespace Tranga; @@ -54,18 +55,21 @@ public class TaskManager string filePath = Path.Join(importFolderPath, "tasks.json"); if (!File.Exists(filePath)) return new HashSet(); - - FileStream file = new FileStream(filePath, FileMode.Open); - TrangaTask[] importTasks = JsonSerializer.Deserialize(file, JsonSerializerOptions.Default)!; + string toRead = File.ReadAllText(filePath); + + TrangaTask[] importTasks = JsonConvert.DeserializeObject(toRead)!; + + foreach(TrangaTask task in importTasks.Where(task => task.publication is not null)) + this._chapterCollection.Add((Publication)task.publication!, new List()); + return importTasks.ToHashSet(); } public void ExportTasks(string exportFolderPath) { - FileStream file = new FileStream(Path.Join(exportFolderPath, "tasks.json"), FileMode.CreateNew); - JsonSerializer.Serialize(file, _allTasks.ToArray(), JsonSerializerOptions.Default); - file.Close(); - file.Dispose(); + string filePath = Path.Join(exportFolderPath, "tasks.json"); + string toWrite = JsonConvert.SerializeObject(_allTasks.ToArray()); + File.WriteAllText(filePath,toWrite); } } \ No newline at end of file diff --git a/Tranga/Tranga.csproj b/Tranga/Tranga.csproj index 6836c68..076e1ee 100644 --- a/Tranga/Tranga.csproj +++ b/Tranga/Tranga.csproj @@ -6,4 +6,8 @@ enable + + + + diff --git a/Tranga/TrangaTask.cs b/Tranga/TrangaTask.cs index 00c1ea9..e0260b7 100644 --- a/Tranga/TrangaTask.cs +++ b/Tranga/TrangaTask.cs @@ -1,15 +1,13 @@ -using System.Text.Json.Serialization; - -namespace Tranga; +namespace Tranga; public struct TrangaTask { - [JsonInclude]public TimeSpan reoccurrence { get; } - [JsonInclude]public DateTime lastExecuted { get; set; } - [JsonInclude]public Connector connector { get; } - [JsonInclude]public Task task { get; } - [JsonInclude]public Publication publication { get; } - [JsonInclude]public string language { get; } + public TimeSpan reoccurrence { get; } + public DateTime lastExecuted { get; set; } + public string connectorName { get; } + public Task task { get; } + public Publication? publication { get; } + public string language { get; } public TrangaTask(Connector connector, Task task, Publication publication, TimeSpan reoccurrence, string language = "") {