Skip to content

Commit 27b6d3d

Browse files
committed
Use StreanWriter #137
1 parent f4d639e commit 27b6d3d

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

src/PicView.Core/Config/SettingsHelper.cs

+14-16
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,13 @@ public static class SettingsHelper
2020

2121
public static async Task LoadSettingsAsync()
2222
{
23+
InitiateJson();
2324
try
2425
{
2526
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config/UserSettings.json");
2627
if (File.Exists(path))
2728
{
2829
var jsonString = await File.ReadAllTextAsync(path).ConfigureAwait(false);
29-
_jsonSerializerOptions ??= new JsonSerializerOptions
30-
{
31-
TypeInfoResolver = SourceGenerationContext.Default,
32-
AllowTrailingCommas = true
33-
};
3430
var settings = JsonSerializer.Deserialize(
3531
jsonString, typeof(AppSettings), SourceGenerationContext.Default)
3632
as AppSettings;
@@ -67,19 +63,25 @@ private static void SetDefaults()
6763
};
6864
}
6965

66+
private static void InitiateJson()
67+
{
68+
_jsonSerializerOptions ??= new JsonSerializerOptions
69+
{
70+
TypeInfoResolver = SourceGenerationContext.Default,
71+
AllowTrailingCommas = true
72+
};
73+
}
74+
7075
public static async Task SaveSettingsAsync()
7176
{
77+
InitiateJson();
7278
try
7379
{
74-
_jsonSerializerOptions ??= new JsonSerializerOptions
75-
{
76-
TypeInfoResolver = SourceGenerationContext.Default,
77-
AllowTrailingCommas = true
78-
};
7980
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config/UserSettings.json");
8081
var updatedJson = JsonSerializer.Serialize(
8182
Settings, typeof(AppSettings), SourceGenerationContext.Default);
82-
await File.WriteAllTextAsync(path, updatedJson).ConfigureAwait(false);
83+
await using var writer = new StreamWriter(path);
84+
await writer.WriteAsync(updatedJson).ConfigureAwait(false);
8385
}
8486
catch (Exception ex)
8587
{
@@ -133,11 +135,7 @@ private static async Task SynchronizeSettings(AppSettings settings)
133135
}
134136

135137
// Save the synchronized settings back to the JSON file
136-
_jsonSerializerOptions ??= new JsonSerializerOptions
137-
{
138-
TypeInfoResolver = SourceGenerationContext.Default,
139-
AllowTrailingCommas = true
140-
};
138+
InitiateJson();
141139
var updatedJson = JsonSerializer.Serialize(
142140
existingSettings, typeof(AppSettings), SourceGenerationContext.Default);
143141
await File.WriteAllTextAsync(path, updatedJson).ConfigureAwait(false);

0 commit comments

Comments
 (0)