44using System ;
55using System . IO ;
66using System . Linq ;
7+ using System . Text . Json ;
8+ using System . Text . Json . Serialization ;
79using System . Xml . Serialization ;
810
911namespace NETworkManager . Settings ;
@@ -30,7 +32,12 @@ public static class SettingsManager
3032 /// <summary>
3133 /// Settings file extension.
3234 /// </summary>
33- private static string SettingsFileExtension => ".xml" ;
35+ private static string SettingsFileExtension => ".json" ;
36+
37+ /// <summary>
38+ /// Legacy XML settings file extension.
39+ /// </summary>
40+ private static string LegacySettingsFileExtension => ".xml" ;
3441
3542 /// <summary>
3643 /// Settings that are currently loaded.
@@ -76,6 +83,15 @@ public static string GetSettingsFilePath()
7683 return Path . Combine ( GetSettingsFolderLocation ( ) , GetSettingsFileName ( ) ) ;
7784 }
7885
86+ /// <summary>
87+ /// Method to get the legacy XML settings file path.
88+ /// </summary>
89+ /// <returns>Legacy XML settings file path.</returns>
90+ private static string GetLegacySettingsFilePath ( )
91+ {
92+ return Path . Combine ( GetSettingsFolderLocation ( ) , $ "{ SettingsFileName } { LegacySettingsFileExtension } ") ;
93+ }
94+
7995 #endregion
8096
8197 #region Initialize, load and save
@@ -99,7 +115,9 @@ public static void Initialize()
99115 public static void Load ( )
100116 {
101117 var filePath = GetSettingsFilePath ( ) ;
118+ var legacyFilePath = GetLegacySettingsFilePath ( ) ;
102119
120+ // Check if JSON file exists
103121 if ( File . Exists ( filePath ) )
104122 {
105123 Current = DeserializeFromFile ( filePath ) ;
@@ -109,16 +127,64 @@ public static void Load()
109127 return ;
110128 }
111129
130+ // Check if legacy XML file exists and migrate it
131+ if ( File . Exists ( legacyFilePath ) )
132+ {
133+ Log . Info ( "Legacy XML settings file found. Migrating to JSON format..." ) ;
134+
135+ Current = DeserializeFromXmlFile ( legacyFilePath ) ;
136+
137+ Current . SettingsChanged = false ;
138+
139+ // Save in new JSON format
140+ Save ( ) ;
141+
142+ // Backup the old XML file
143+ var backupFilePath = Path . Combine ( GetSettingsFolderLocation ( ) ,
144+ $ "{ SettingsFileName } { LegacySettingsFileExtension } .backup") ;
145+ File . Copy ( legacyFilePath , backupFilePath , true ) ;
146+ Log . Info ( $ "Legacy XML settings file backed up to: { backupFilePath } ") ;
147+
148+ // Optionally, delete the original XML file after successful migration
149+ // File.Delete(legacyFilePath);
150+
151+ Log . Info ( "Settings migration from XML to JSON completed successfully." ) ;
152+
153+ return ;
154+ }
155+
112156 // Initialize the default settings if there is no settings file.
113157 Initialize ( ) ;
114158 }
115159
116160 /// <summary>
117- /// Method to deserialize the settings from a file.
161+ /// Method to deserialize the settings from a JSON file.
118162 /// </summary>
119163 /// <param name="filePath">Path to the settings file.</param>
120164 /// <returns>Settings as <see cref="SettingsInfo" />.</returns>
121165 private static SettingsInfo DeserializeFromFile ( string filePath )
166+ {
167+ var jsonString = File . ReadAllText ( filePath ) ;
168+
169+ var options = new JsonSerializerOptions
170+ {
171+ WriteIndented = true ,
172+ PropertyNameCaseInsensitive = true ,
173+ DefaultIgnoreCondition = JsonIgnoreCondition . Never ,
174+ Converters = { new JsonStringEnumConverter ( ) }
175+ } ;
176+
177+ var settingsInfo = JsonSerializer . Deserialize < SettingsInfo > ( jsonString , options ) ;
178+
179+ return settingsInfo ;
180+ }
181+
182+ /// <summary>
183+ /// Method to deserialize the settings from a legacy XML file.
184+ /// </summary>
185+ /// <param name="filePath">Path to the XML settings file.</param>
186+ /// <returns>Settings as <see cref="SettingsInfo" />.</returns>
187+ private static SettingsInfo DeserializeFromXmlFile ( string filePath )
122188 {
123189 var xmlSerializer = new XmlSerializer ( typeof ( SettingsInfo ) ) ;
124190
@@ -145,16 +211,21 @@ public static void Save()
145211 }
146212
147213 /// <summary>
148- /// Method to serialize the settings to a file.
214+ /// Method to serialize the settings to a JSON file.
149215 /// </summary>
150216 /// <param name="filePath">Path to the settings file.</param>
151217 private static void SerializeToFile ( string filePath )
152218 {
153- var xmlSerializer = new XmlSerializer ( typeof ( SettingsInfo ) ) ;
219+ var options = new JsonSerializerOptions
220+ {
221+ WriteIndented = true ,
222+ DefaultIgnoreCondition = JsonIgnoreCondition . Never ,
223+ Converters = { new JsonStringEnumConverter ( ) }
224+ } ;
154225
155- using var fileStream = new FileStream ( filePath , FileMode . Create ) ;
226+ var jsonString = JsonSerializer . Serialize ( Current , options ) ;
156227
157- xmlSerializer . Serialize ( fileStream , Current ) ;
228+ File . WriteAllText ( filePath , jsonString ) ;
158229 }
159230
160231 #endregion
0 commit comments