mirror of
https://github.com/terrymacdonald/DisplayMagician.git
synced 2024-08-30 18:32:20 +00:00
Standardised JSON.net settings across modules
Just noticed that the Settings, Display Profiles and Game Shortcuts all had different json.net settings for reading and writing across all the modules. Have now standardised across them to make them act the same way.
This commit is contained in:
parent
30ef263e75
commit
d719cecad2
@ -202,8 +202,20 @@ namespace DisplayMagician {
|
||||
string oldv1SettingsFile = Path.Combine(AppDataPath, "Settings_1.0.json");
|
||||
string oldv2SettingsFile = Path.Combine(AppDataPath, "Settings_2.0.json");
|
||||
string oldv23SettingsFile = Path.Combine(AppDataPath, "Settings_2.3.json");
|
||||
string oldv24SettingsFile = Path.Combine(AppDataPath, "Settings_2.4.json");
|
||||
|
||||
if (File.Exists(oldv23SettingsFile))
|
||||
if (File.Exists(oldv24SettingsFile))
|
||||
{
|
||||
File.Copy(oldv24SettingsFile, targetSettingsFile, true);
|
||||
upgradedSettingsFile = true;
|
||||
|
||||
// Load the program settings to populate the extra additional settings with default values
|
||||
// as there are some new settings in there.
|
||||
AppProgramSettings = ProgramSettings.LoadSettings();
|
||||
// Save the updated program settings so they're baked in.
|
||||
AppProgramSettings.SaveSettings();
|
||||
}
|
||||
else if (File.Exists(oldv23SettingsFile))
|
||||
{
|
||||
File.Copy(oldv23SettingsFile, targetSettingsFile, true);
|
||||
upgradedSettingsFile = true;
|
||||
@ -451,18 +463,24 @@ namespace DisplayMagician {
|
||||
{
|
||||
string oldv1ShortcutsFile = Path.Combine(AppShortcutPath, "Shortcuts_1.0.json");
|
||||
string oldv2ShortcutsFile = Path.Combine(AppShortcutPath, "Shortcuts_2.0.json");
|
||||
string oldv22ShortcutsFile = Path.Combine(AppShortcutPath, "Shortcuts_2.2.json");
|
||||
|
||||
if (File.Exists(oldv2ShortcutsFile))
|
||||
if (File.Exists(oldv22ShortcutsFile))
|
||||
{
|
||||
logger.Info($"Program/Main: Upgrading v1 shortcut file {oldv2ShortcutsFile} to v2.2 shortcut file {targetShortcutsFile}.");
|
||||
logger.Info($"Program/Main: Upgrading v2.2 shortcut file {oldv2ShortcutsFile} to latest shortcut file {targetShortcutsFile}.");
|
||||
File.Copy(oldv2ShortcutsFile, targetShortcutsFile);
|
||||
}
|
||||
else if (File.Exists(oldv2ShortcutsFile))
|
||||
{
|
||||
logger.Info($"Program/Main: Upgrading v2.0 shortcut file {oldv2ShortcutsFile} to latest shortcut file {targetShortcutsFile}.");
|
||||
File.Copy(oldv2ShortcutsFile, targetShortcutsFile);
|
||||
}
|
||||
else if (File.Exists(oldv1ShortcutsFile))
|
||||
{
|
||||
logger.Info($"Program/Main: Upgrading v1 shortcut file {oldv1ShortcutsFile} to v2.2 shortcut file {targetShortcutsFile}.");
|
||||
logger.Info($"Program/Main: Upgrading v1.0 shortcut file {oldv1ShortcutsFile} to latest shortcut file {targetShortcutsFile}.");
|
||||
File.Copy(oldv1ShortcutsFile, targetShortcutsFile);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -1,10 +1,12 @@
|
||||
using Newtonsoft.Json;
|
||||
using DisplayMagicianShared;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
using Windows.Data.Json;
|
||||
|
||||
|
||||
namespace DisplayMagician
|
||||
@ -16,7 +18,7 @@ namespace DisplayMagician
|
||||
// Common items to the class
|
||||
private static bool _programSettingsLoaded = false;
|
||||
// Other constants that are useful
|
||||
public static string programSettingsStorageJsonFileName = Path.Combine(Program.AppDataPath, $"Settings_2.4.json");
|
||||
public static string programSettingsStorageJsonFileName = Path.Combine(Program.AppDataPath, $"Settings_2.5.json");
|
||||
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
#endregion
|
||||
|
||||
@ -268,9 +270,10 @@ namespace DisplayMagician
|
||||
if (File.Exists(programSettingsStorageJsonFileName))
|
||||
{
|
||||
string json = "";
|
||||
List<string> jsonErrors = new List<string>();
|
||||
try {
|
||||
json = File.ReadAllText(programSettingsStorageJsonFileName, Encoding.Unicode);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"ProgramSettings/LoadSettings: Tried to read the JSON file {programSettingsStorageJsonFileName} to memory from disk but File.ReadAllText threw an exception. {ex}");
|
||||
@ -284,13 +287,43 @@ namespace DisplayMagician
|
||||
{
|
||||
MissingMemberHandling = MissingMemberHandling.Ignore,
|
||||
NullValueHandling = NullValueHandling.Ignore,
|
||||
DefaultValueHandling = DefaultValueHandling.Include,
|
||||
TypeNameHandling = TypeNameHandling.Auto
|
||||
DefaultValueHandling = DefaultValueHandling.Populate,
|
||||
TypeNameHandling = TypeNameHandling.Auto,
|
||||
ObjectCreationHandling = ObjectCreationHandling.Replace,
|
||||
Error = delegate (object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args)
|
||||
{
|
||||
jsonErrors.Add($"JSON.net Error: {args.ErrorContext.Error.Source}:{args.ErrorContext.Error.StackTrace} - {args.ErrorContext.Error.Message} | InnerException:{args.ErrorContext.Error.InnerException.Source}:{args.ErrorContext.Error.InnerException.StackTrace} - {args.ErrorContext.Error.InnerException.Message}");
|
||||
args.ErrorContext.Handled = true;
|
||||
},
|
||||
});
|
||||
}
|
||||
catch (JsonReaderException ex)
|
||||
{
|
||||
// If there is a error in the JSON format
|
||||
if (ex.HResult == -2146233088)
|
||||
{
|
||||
SharedLogger.logger.Error(ex, $"ProgramSettings/LoadSettings: JSONReaderException - The Program Settings file {programSettingsStorageJsonFileName} contains a syntax error. Please check the file for correctness with a JSON validator.");
|
||||
}
|
||||
else
|
||||
{
|
||||
SharedLogger.logger.Error(ex, $"ProgramSettings/LoadSettings: JSONReaderException while trying to process the Program Settings file {programSettingsStorageJsonFileName} but JsonConvert threw an exception.");
|
||||
}
|
||||
MessageBox.Show($"The Program Settings file {programSettingsStorageJsonFileName} contains a syntax error. Please check the file for correctness with a JSON validator.", "Error loading the Program Settings", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"ProgramSettings/LoadSettings: Tried to parse the JSON file {programSettingsStorageJsonFileName} but the JsonConvert threw an exception. {ex}");
|
||||
SharedLogger.logger.Error(ex, $"ProgramSettings/LoadSettings: Tried to parse the JSON in the Program Settings file {programSettingsStorageJsonFileName} but the JsonConvert threw an exception.");
|
||||
MessageBox.Show($"The Program Settings file {programSettingsStorageJsonFileName} contains a syntax error. Please check the file for correctness with a JSON validator.", "Error loading the Program Settings", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
|
||||
// If we have any JSON.net errors, then we need to records them in the logs
|
||||
if (jsonErrors.Count > 0)
|
||||
{
|
||||
foreach (string jsonError in jsonErrors)
|
||||
{
|
||||
SharedLogger.logger.Error($"ProgramSettings/LoadSettings: {jsonErrors}");
|
||||
}
|
||||
}
|
||||
|
||||
if (programSettings.DisplayMagicianVersion == null) {
|
||||
@ -321,14 +354,22 @@ namespace DisplayMagician
|
||||
// Force the PreviousDisplayMagicianVersion to this version just before we save the settings.
|
||||
DisplayMagicianVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
|
||||
|
||||
List<string> jsonErrors = new List<string>();
|
||||
try
|
||||
{
|
||||
|
||||
var json = JsonConvert.SerializeObject(this, Formatting.Indented, new JsonSerializerSettings
|
||||
{
|
||||
NullValueHandling = NullValueHandling.Include,
|
||||
DefaultValueHandling = DefaultValueHandling.Populate,
|
||||
TypeNameHandling = TypeNameHandling.Auto
|
||||
|
||||
DefaultValueHandling = DefaultValueHandling.Include,
|
||||
TypeNameHandling = TypeNameHandling.Auto,
|
||||
MissingMemberHandling = MissingMemberHandling.Error,
|
||||
ObjectCreationHandling = ObjectCreationHandling.Replace,
|
||||
Error = delegate (object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args)
|
||||
{
|
||||
jsonErrors.Add($"JSON.net Error: {args.ErrorContext.Error.Source}:{args.ErrorContext.Error.StackTrace} - {args.ErrorContext.Error.Message} | InnerException:{args.ErrorContext.Error.InnerException.Source}:{args.ErrorContext.Error.InnerException.StackTrace} - {args.ErrorContext.Error.InnerException.Message}");
|
||||
args.ErrorContext.Handled = true;
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@ -343,6 +384,15 @@ namespace DisplayMagician
|
||||
logger.Error(ex, $"ProgramSettings/SaveSettings: Exception attempting to save the program settings to {programSettingsStorageJsonFileName}.");
|
||||
}
|
||||
|
||||
// If we have any JSON.net errors, then we need to records them in the logs
|
||||
if (jsonErrors.Count > 0)
|
||||
{
|
||||
foreach (string jsonError in jsonErrors)
|
||||
{
|
||||
logger.Error($"ProgramSettings/SaveSettings: {jsonErrors}");
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -40,7 +40,7 @@ namespace DisplayMagician
|
||||
//private static bool _cancelWait = false;
|
||||
// Other constants that are useful
|
||||
private static string AppShortcutStoragePath = Path.Combine(Program.AppDataPath, $"Shortcuts");
|
||||
private static string _shortcutStorageJsonFileName = Path.Combine(AppShortcutStoragePath, $"Shortcuts_2.2.json");
|
||||
private static string _shortcutStorageJsonFileName = Path.Combine(AppShortcutStoragePath, $"Shortcuts_2.5.json");
|
||||
private static string uuidV4Regex = @"(?im)^[{(]?[0-9A-F]{8}[-]?(?:[0-9A-F]{4}[-]?){3}[0-9A-F]{12}[)}]?$";
|
||||
private static CoreAudioController _audioController = null;
|
||||
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
@ -466,6 +466,7 @@ namespace DisplayMagician
|
||||
NullValueHandling = NullValueHandling.Ignore,
|
||||
DefaultValueHandling = DefaultValueHandling.Populate,
|
||||
TypeNameHandling = TypeNameHandling.Auto,
|
||||
ObjectCreationHandling = ObjectCreationHandling.Replace,
|
||||
Error = delegate (object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args)
|
||||
{
|
||||
jsonErrors.Add($"JSON.net Error: {args.ErrorContext.Error.Source}:{args.ErrorContext.Error.StackTrace} - {args.ErrorContext.Error.Message} | InnerException:{args.ErrorContext.Error.InnerException.Source}:{args.ErrorContext.Error.InnerException.StackTrace} - {args.ErrorContext.Error.InnerException.Message}");
|
||||
@ -620,8 +621,10 @@ namespace DisplayMagician
|
||||
JsonSerializerSettings mySerializerSettings = new JsonSerializerSettings
|
||||
{
|
||||
NullValueHandling = NullValueHandling.Include,
|
||||
DefaultValueHandling = DefaultValueHandling.Populate,
|
||||
DefaultValueHandling = DefaultValueHandling.Include,
|
||||
TypeNameHandling = TypeNameHandling.Auto,
|
||||
MissingMemberHandling = MissingMemberHandling.Error,
|
||||
ObjectCreationHandling = ObjectCreationHandling.Replace,
|
||||
Error = delegate (object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args)
|
||||
{
|
||||
jsonErrors.Add($"JSON.net Error: {args.ErrorContext.Error.Source}:{args.ErrorContext.Error.StackTrace} - {args.ErrorContext.Error.Message} | InnerException:{args.ErrorContext.Error.InnerException.Source}:{args.ErrorContext.Error.InnerException.StackTrace} - {args.ErrorContext.Error.InnerException.Message}");
|
||||
@ -644,7 +647,7 @@ namespace DisplayMagician
|
||||
logger.Error(ex, $"ShortcutRepository/SaveShortcuts: Unable to save the shortcut repository to the {_shortcutStorageJsonFileName}.");
|
||||
}
|
||||
|
||||
// If we have any JSON.net errors, then we need to records them in the logs
|
||||
// If we have any JSON.net errors, then we need to record them in the logs
|
||||
if (jsonErrors.Count > 0)
|
||||
{
|
||||
foreach (string jsonError in jsonErrors)
|
||||
|
@ -839,10 +839,9 @@ namespace DisplayMagicianShared
|
||||
{
|
||||
MissingMemberHandling = MissingMemberHandling.Ignore,
|
||||
NullValueHandling = NullValueHandling.Include,
|
||||
DefaultValueHandling = DefaultValueHandling.Include,
|
||||
DefaultValueHandling = DefaultValueHandling.Populate,
|
||||
TypeNameHandling = TypeNameHandling.Auto,
|
||||
ObjectCreationHandling = ObjectCreationHandling.Replace,
|
||||
|
||||
ObjectCreationHandling = ObjectCreationHandling.Replace,
|
||||
Error = delegate (object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args)
|
||||
{
|
||||
jsonErrors.Add($"JSON.net Error: {args.ErrorContext.Error.Source}:{args.ErrorContext.Error.StackTrace} - {args.ErrorContext.Error.Message} | InnerException:{args.ErrorContext.Error.InnerException.Source}:{args.ErrorContext.Error.InnerException.StackTrace} - {args.ErrorContext.Error.InnerException.Message}");
|
||||
@ -1003,7 +1002,7 @@ namespace DisplayMagicianShared
|
||||
json = root.ToString(Formatting.Indented);
|
||||
if (!string.IsNullOrWhiteSpace(json))
|
||||
{
|
||||
SharedLogger.logger.Debug($"ProfileRepository/SaveProfiles: Saving the profile repository to the {_profileStorageJsonFileName}.");
|
||||
SharedLogger.logger.Debug($"ProfileRepository/MigrateJsonToLatestVersion: Saving the profile repository to the {_profileStorageJsonFileName}.");
|
||||
|
||||
File.WriteAllText(_profileStorageJsonFileName, json, Encoding.Unicode);
|
||||
}
|
||||
@ -1054,9 +1053,7 @@ namespace DisplayMagicianShared
|
||||
JsonSerializerSettings mySerializerSettings = new JsonSerializerSettings
|
||||
{
|
||||
NullValueHandling = NullValueHandling.Include,
|
||||
//NullValueHandling = NullValueHandling.Ignore,
|
||||
DefaultValueHandling = DefaultValueHandling.Include,
|
||||
//DefaultValueHandling = DefaultValueHandling.Ignore,
|
||||
TypeNameHandling = TypeNameHandling.Auto,
|
||||
MissingMemberHandling = MissingMemberHandling.Error,
|
||||
ObjectCreationHandling = ObjectCreationHandling.Replace,
|
||||
|
Loading…
Reference in New Issue
Block a user