Skip to content

Commit 47faca2

Browse files
CopilotBornToBeRoot
andcommitted
Address code review comments: refactor JSON options and add null check
Co-authored-by: BornToBeRoot <[email protected]>
1 parent 68874f2 commit 47faca2

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

Source/NETworkManager.Settings/SettingsManager.cs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ public static class SettingsManager
4949
/// </summary>
5050
public static bool HotKeysChanged { get; set; }
5151

52+
/// <summary>
53+
/// JSON serializer options for consistent serialization/deserialization.
54+
/// </summary>
55+
private static readonly JsonSerializerOptions JsonOptions = new()
56+
{
57+
WriteIndented = true,
58+
PropertyNameCaseInsensitive = true,
59+
DefaultIgnoreCondition = JsonIgnoreCondition.Never,
60+
Converters = { new JsonStringEnumConverter() }
61+
};
62+
5263
#endregion
5364

5465
#region Settings location, default paths and file names
@@ -145,8 +156,8 @@ public static void Load()
145156
File.Copy(legacyFilePath, backupFilePath, true);
146157
Log.Info($"Legacy XML settings file backed up to: {backupFilePath}");
147158

148-
// Optionally, delete the original XML file after successful migration
149-
// File.Delete(legacyFilePath);
159+
// Note: The original XML file is intentionally not deleted to allow users to revert if needed.
160+
// Users can manually delete Settings.xml after verifying the migration was successful.
150161

151162
Log.Info("Settings migration from XML to JSON completed successfully.");
152163

@@ -166,15 +177,12 @@ private static SettingsInfo DeserializeFromFile(string filePath)
166177
{
167178
var jsonString = File.ReadAllText(filePath);
168179

169-
var options = new JsonSerializerOptions
170-
{
171-
WriteIndented = true,
172-
PropertyNameCaseInsensitive = true,
173-
DefaultIgnoreCondition = JsonIgnoreCondition.Never,
174-
Converters = { new JsonStringEnumConverter() }
175-
};
180+
var settingsInfo = JsonSerializer.Deserialize<SettingsInfo>(jsonString, JsonOptions);
176181

177-
var settingsInfo = JsonSerializer.Deserialize<SettingsInfo>(jsonString, options);
182+
if (settingsInfo == null)
183+
{
184+
throw new InvalidOperationException("Failed to deserialize settings from JSON file. The result was null.");
185+
}
178186

179187
return settingsInfo;
180188
}
@@ -216,14 +224,7 @@ public static void Save()
216224
/// <param name="filePath">Path to the settings file.</param>
217225
private static void SerializeToFile(string filePath)
218226
{
219-
var options = new JsonSerializerOptions
220-
{
221-
WriteIndented = true,
222-
DefaultIgnoreCondition = JsonIgnoreCondition.Never,
223-
Converters = { new JsonStringEnumConverter() }
224-
};
225-
226-
var jsonString = JsonSerializer.Serialize(Current, options);
227+
var jsonString = JsonSerializer.Serialize(Current, JsonOptions);
227228

228229
File.WriteAllText(filePath, jsonString);
229230
}

0 commit comments

Comments
 (0)