Skip to content

Commit

Permalink
fix: Trim settings json of additional chars on load
Browse files Browse the repository at this point in the history
(personal?) issue where sometimes additional null characters get saved to the end of the json file, which throws an error on deserializing the file...
  • Loading branch information
lulzsun committed Jan 8, 2025
1 parent ba8383f commit a0bb5f0
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion Classes/Services/SettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.IO;
using System.Management;
using System.Text;
using System.Text.Json;
using static RePlays.Utils.Functions;

Expand Down Expand Up @@ -38,7 +39,15 @@ public class SettingsJson {
public static void LoadSettings() {
if (File.Exists(settingsFile)) {
try {
_Settings = JsonSerializer.Deserialize<SettingsJson>(File.ReadAllText(settingsFile));
var content = File.ReadAllText(settingsFile);

// Trim characters after the last closing bracket
// Sometimes a bunch of junk chars gets saved after serializing for unknown reason...
// Maybe because my settings json is located on a winbtrs drive and is corrupting it?
// Not sure, but this fixes it...
if (content.LastIndexOf('}') != -1) content = content[..(content.LastIndexOf('}') + 1)];

_Settings = JsonSerializer.Deserialize<SettingsJson>(content);
Logger.WriteLine("Loaded userSettings.json");
}
catch (JsonException ex) {
Expand Down

0 comments on commit a0bb5f0

Please sign in to comment.