Switch to Newtonsoft.Json for serialization

This commit is contained in:
glax 2023-05-19 16:23:37 +02:00
parent 0afbcd9bbf
commit afe36ab2ef
4 changed files with 27 additions and 22 deletions

View File

@ -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

View File

@ -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<TrangaTask>();
FileStream file = new FileStream(filePath, FileMode.Open);
TrangaTask[] importTasks = JsonSerializer.Deserialize<TrangaTask[]>(file, JsonSerializerOptions.Default)!;
string toRead = File.ReadAllText(filePath);
TrangaTask[] importTasks = JsonConvert.DeserializeObject<TrangaTask[]>(toRead)!;
foreach(TrangaTask task in importTasks.Where(task => task.publication is not null))
this._chapterCollection.Add((Publication)task.publication!, new List<Chapter>());
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);
}
}

View File

@ -6,4 +6,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project>

View File

@ -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 = "")
{