@@ -76,7 +76,7 @@ public SettingsManager(string? settingsFilePath = null)
7676            throw  new  ArgumentException ( "settingsFilePath must be an absolute path if provided" ,  nameof ( settingsFilePath ) ) ; 
7777        } 
7878
79-         string  folder  =  Path . Combine ( 
79+         var  folder  =  Path . Combine ( 
8080                settingsFilePath , 
8181                _appName ) ; 
8282
@@ -86,9 +86,8 @@ public SettingsManager(string? settingsFilePath = null)
8686        if  ( ! File . Exists ( _settingsFilePath ) ) 
8787        { 
8888            // Create the settings file if it doesn't exist 
89-             string  emptyJson  =  JsonSerializer . Serialize ( new  {  } ) ; 
90-             File . WriteAllText ( _settingsFilePath ,  emptyJson ) ; 
9189            _settings  =  new ( ) ; 
90+             File . WriteAllText ( _settingsFilePath ,  JsonSerializer . Serialize ( _settings ,  SettingsJsonContext . Default . Settings ) ) ; 
9291        } 
9392        else 
9493        { 
@@ -109,12 +108,12 @@ private void Save(string name, bool value)
109108                    FileShare . None ) ; 
110109
111110                // Ensure cache is loaded before saving  
112-                 var  freshCache  =  JsonSerializer . Deserialize < Settings > ( fs )  ??  new ( ) ; 
111+                 var  freshCache  =  JsonSerializer . Deserialize ( fs ,   SettingsJsonContext . Default . Settings )  ??  new ( ) ; 
113112                _settings  =  freshCache ; 
114113                _settings . Options [ name ]  =  JsonSerializer . SerializeToElement ( value ) ; 
115114                fs . Position  =  0 ;  // Reset stream position to the beginning before writing 
116115
117-                 JsonSerializer . Serialize ( fs ,  _settings ,  new   JsonSerializerOptions   {   WriteIndented   =   true   } ) ; 
116+                 JsonSerializer . Serialize ( fs ,  _settings ,  SettingsJsonContext . Default . Settings ) ; 
118117
119118                // This ensures the file is truncated to the new length 
120119                // if the new content is shorter than the old content 
@@ -152,33 +151,39 @@ private Settings Load()
152151        try 
153152        { 
154153            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 ( ) ; 
156155        } 
157156        catch  ( Exception  ex ) 
158157        { 
159158            throw  new  InvalidOperationException ( $ "Failed to load settings from { _settingsFilePath } . The file may be corrupted or malformed. Exception: { ex . Message } ") ; 
160159        } 
161160    } 
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 ;  } 
162172
163-     [ JsonSerializable ( typeof ( Settings ) ) ] 
164-     private   class   Settings 
173+     private   const   int   VERSION   =   1 ;   // Default version for backward compatibility 
174+     public   Settings ( ) 
165175    { 
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+     } 
177179
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 ; 
183184    } 
184185} 
186+ 
187+ [ JsonSerializable ( typeof ( Settings ) ) ] 
188+ [ JsonSourceGenerationOptions ( WriteIndented  =  true ) ] 
189+ public  partial  class  SettingsJsonContext  :  JsonSerializerContext ; 
0 commit comments