@@ -20,17 +20,13 @@ public static class SettingsHelper
20
20
21
21
public static async Task LoadSettingsAsync ( )
22
22
{
23
+ InitiateJson ( ) ;
23
24
try
24
25
{
25
26
var path = Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , "Config/UserSettings.json" ) ;
26
27
if ( File . Exists ( path ) )
27
28
{
28
29
var jsonString = await File . ReadAllTextAsync ( path ) . ConfigureAwait ( false ) ;
29
- _jsonSerializerOptions ??= new JsonSerializerOptions
30
- {
31
- TypeInfoResolver = SourceGenerationContext . Default ,
32
- AllowTrailingCommas = true
33
- } ;
34
30
var settings = JsonSerializer . Deserialize (
35
31
jsonString , typeof ( AppSettings ) , SourceGenerationContext . Default )
36
32
as AppSettings ;
@@ -67,19 +63,25 @@ private static void SetDefaults()
67
63
} ;
68
64
}
69
65
66
+ private static void InitiateJson ( )
67
+ {
68
+ _jsonSerializerOptions ??= new JsonSerializerOptions
69
+ {
70
+ TypeInfoResolver = SourceGenerationContext . Default ,
71
+ AllowTrailingCommas = true
72
+ } ;
73
+ }
74
+
70
75
public static async Task SaveSettingsAsync ( )
71
76
{
77
+ InitiateJson ( ) ;
72
78
try
73
79
{
74
- _jsonSerializerOptions ??= new JsonSerializerOptions
75
- {
76
- TypeInfoResolver = SourceGenerationContext . Default ,
77
- AllowTrailingCommas = true
78
- } ;
79
80
var path = Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , "Config/UserSettings.json" ) ;
80
81
var updatedJson = JsonSerializer . Serialize (
81
82
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 ) ;
83
85
}
84
86
catch ( Exception ex )
85
87
{
@@ -133,11 +135,7 @@ private static async Task SynchronizeSettings(AppSettings settings)
133
135
}
134
136
135
137
// Save the synchronized settings back to the JSON file
136
- _jsonSerializerOptions ??= new JsonSerializerOptions
137
- {
138
- TypeInfoResolver = SourceGenerationContext . Default ,
139
- AllowTrailingCommas = true
140
- } ;
138
+ InitiateJson ( ) ;
141
139
var updatedJson = JsonSerializer . Serialize (
142
140
existingSettings , typeof ( AppSettings ) , SourceGenerationContext . Default ) ;
143
141
await File . WriteAllTextAsync ( path , updatedJson ) . ConfigureAwait ( false ) ;
0 commit comments