mirror of
https://github.com/denglihong2007/CRSim
synced 2026-08-06 17:53:17 +08:00
fix: 无法加载/保存数据
This commit is contained in:
10
CRSim.Core/Models/JsonSerializationContext.cs
Normal file
10
CRSim.Core/Models/JsonSerializationContext.cs
Normal 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
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user