From 46ec868134dcff92ddd4f884630b53fdd6d8eb35 Mon Sep 17 00:00:00 2001 From: denglihong2007 <98096191+denglihong2007@users.noreply.github.com> Date: Sun, 13 Jul 2025 14:33:32 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=97=A0=E6=B3=95=E5=8A=A0=E8=BD=BD/?= =?UTF-8?q?=E4=BF=9D=E5=AD=98=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CRSim.Core/Models/JsonSerializationContext.cs | 10 ++++++++++ CRSim.Core/Services/DatabaseService.cs | 12 +++--------- CRSim/CRSim.csproj | 7 ++++++- 3 files changed, 19 insertions(+), 10 deletions(-) create mode 100644 CRSim.Core/Models/JsonSerializationContext.cs 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