@@ -76,7 +76,7 @@ public SettingsManager(string? settingsFilePath = null)
76
76
throw new ArgumentException ( "settingsFilePath must be an absolute path if provided" , nameof ( settingsFilePath ) ) ;
77
77
}
78
78
79
- string folder = Path . Combine (
79
+ var folder = Path . Combine (
80
80
settingsFilePath ,
81
81
_appName ) ;
82
82
@@ -86,9 +86,8 @@ public SettingsManager(string? settingsFilePath = null)
86
86
if ( ! File . Exists ( _settingsFilePath ) )
87
87
{
88
88
// Create the settings file if it doesn't exist
89
- string emptyJson = JsonSerializer . Serialize ( new { } ) ;
90
- File . WriteAllText ( _settingsFilePath , emptyJson ) ;
91
89
_settings = new ( ) ;
90
+ File . WriteAllText ( _settingsFilePath , JsonSerializer . Serialize ( _settings , SettingsJsonContext . Default . Settings ) ) ;
92
91
}
93
92
else
94
93
{
@@ -109,12 +108,12 @@ private void Save(string name, bool value)
109
108
FileShare . None ) ;
110
109
111
110
// Ensure cache is loaded before saving
112
- var freshCache = JsonSerializer . Deserialize < Settings > ( fs ) ?? new ( ) ;
111
+ var freshCache = JsonSerializer . Deserialize ( fs , SettingsJsonContext . Default . Settings ) ?? new ( ) ;
113
112
_settings = freshCache ;
114
113
_settings . Options [ name ] = JsonSerializer . SerializeToElement ( value ) ;
115
114
fs . Position = 0 ; // Reset stream position to the beginning before writing
116
115
117
- JsonSerializer . Serialize ( fs , _settings , new JsonSerializerOptions { WriteIndented = true } ) ;
116
+ JsonSerializer . Serialize ( fs , _settings , SettingsJsonContext . Default . Settings ) ;
118
117
119
118
// This ensures the file is truncated to the new length
120
119
// if the new content is shorter than the old content
@@ -152,33 +151,39 @@ private Settings Load()
152
151
try
153
152
{
154
153
using var fs = File . OpenRead ( _settingsFilePath ) ;
155
- return JsonSerializer . Deserialize < Settings > ( fs ) ?? new ( null , new Dictionary < string , JsonElement > ( ) ) ;
154
+ return JsonSerializer . Deserialize ( fs , SettingsJsonContext . Default . Settings ) ?? new ( ) ;
156
155
}
157
156
catch ( Exception ex )
158
157
{
159
158
throw new InvalidOperationException ( $ "Failed to load settings from{ _settingsFilePath } . The file may be corrupted or malformed. Exception:{ ex . Message } ") ;
160
159
}
161
160
}
161
+ }
162
+
163
+ public class Settings
164
+ {
165
+ /// <summary>
166
+ /// User settings version. Increment this when the settings schema changes.
167
+ /// In future iterations we will be able to handle migrations when the user has
168
+ /// an older version.
169
+ /// </summary>
170
+ public int Version { get ; set ; }
171
+ public Dictionary < string , JsonElement > Options { get ; set ; }
162
172
163
- [ JsonSerializable ( typeof ( Settings ) ) ]
164
- private class Settings
173
+ private const int VERSION = 1 ; // Default version for backward compatibility
174
+ public Settings ( )
165
175
{
166
- /// <summary>
167
- /// User settings version. Increment this when the settings schema changes.
168
- /// In future iterations we will be able to handle migrations when the user has
169
- /// an older version.
170
- /// </summary>
171
- public int Version { get ; set ; } = 1 ;
172
- public Dictionary < string , JsonElement > Options { get ; set ; }
173
- public Settings ( )
174
- {
175
- Options = new Dictionary < string , JsonElement > ( ) ;
176
- }
176
+ Version = VERSION ;
177
+ Options = [ ] ;
178
+ }
177
179
178
- public Settings ( int ? version , Dictionary < string , JsonElement > options )
179
- {
180
- Version = version ?? Version ;
181
- Options = options ;
182
- }
180
+ public Settings ( int ? version , Dictionary < string , JsonElement > options )
181
+ {
182
+ Version = version ?? VERSION ;
183
+ Options = options ;
183
184
}
184
185
}
186
+
187
+ [ JsonSerializable ( typeof ( Settings ) ) ]
188
+ [ JsonSourceGenerationOptions ( WriteIndented = true ) ]
189
+ public partial class SettingsJsonContext : JsonSerializerContext ;