diff --git a/CRSim.Core/Models/JsonSerializationContext.cs b/CRSim.Core/Models/JsonSerializationContext.cs new file mode 100644 index 0000000..8f5691a --- /dev/null +++ b/CRSim.Core/Models/JsonSerializationContext.cs @@ -0,0 +1,10 @@ +using System.Text.Json.Serialization; + +namespace CRSim.Core.Models +{ + [JsonSerializable(typeof(Json))] + [JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)] + public partial class JsonContext : JsonSerializerContext + { + } +} diff --git a/CRSim.Core/Services/DatabaseService.cs b/CRSim.Core/Services/DatabaseService.cs index 217fe43..c4719ac 100644 --- a/CRSim.Core/Services/DatabaseService.cs +++ b/CRSim.Core/Services/DatabaseService.cs @@ -11,12 +11,6 @@ namespace CRSim.Core.Services private List _stations; private List _trainNumbers; - private readonly JsonSerializerOptions options = new() - { - WriteIndented = true, - DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, - }; - public DatabaseService() { ImportData(AppPaths.ConfigFilePath); @@ -32,7 +26,7 @@ namespace CRSim.Core.Services try { var json = File.ReadAllText(jsonFilePath).Replace("StationStop", "TrainStop");// 修复旧版本的数据 - var data = JsonSerializer.Deserialize(json); + var data = JsonSerializer.Deserialize(json,JsonContext.Default.Json); _stations = data.Stations; _trainNumbers = data.TrainNumbers; @@ -108,7 +102,7 @@ namespace CRSim.Core.Services { Stations = _stations, TrainNumbers = _trainNumbers - }, options); + },JsonContext.Default.Json); await File.WriteAllTextAsync(AppPaths.ConfigFilePath, json); } @@ -118,7 +112,7 @@ namespace CRSim.Core.Services { Stations = _stations, TrainNumbers = _trainNumbers - }, options); + }, JsonContext.Default.Json); await File.WriteAllTextAsync(p, json); } diff --git a/CRSim/CRSim.csproj b/CRSim/CRSim.csproj index e6923ce..f8253ba 100644 --- a/CRSim/CRSim.csproj +++ b/CRSim/CRSim.csproj @@ -13,7 +13,7 @@ enable None preview - 2.3.101.0 + 2.3.101.1 @@ -92,6 +92,11 @@ + + + PreserveNewest + + True