fix: 无法加载/保存数据

This commit is contained in:
denglihong2007
2025-07-13 14:33:32 +08:00
parent 63be26ef9e
commit 46ec868134
3 changed files with 19 additions and 10 deletions

View File

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

View File

@@ -11,12 +11,6 @@ namespace CRSim.Core.Services
private List<Station> _stations;
private List<TrainNumber> _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>(json);
var data = JsonSerializer.Deserialize<Json>(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);
}

View File

@@ -13,7 +13,7 @@
<Nullable>enable</Nullable>
<WindowsPackageType>None</WindowsPackageType>
<LangVersion>preview</LangVersion>
<Version>2.3.101.0</Version>
<Version>2.3.101.1</Version>
</PropertyGroup>
<ItemGroup>
<Content Remove="Assets\CRSimIcon.png" />
@@ -92,6 +92,11 @@
<ItemGroup>
<Folder Include="Services\" />
</ItemGroup>
<ItemGroup>
<Content Update="Assets\CRSimIcon.ico">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<None Update="Assets\CRSimIcon.png">
<Pack>True</Pack>