diff --git a/Pilz.Configuration/SettingsManager.cs b/Pilz.Configuration/SettingsManager.cs index 9e2539f..1615b5b 100644 --- a/Pilz.Configuration/SettingsManager.cs +++ b/Pilz.Configuration/SettingsManager.cs @@ -2,6 +2,8 @@ using System.Collections.Generic; using System.IO; using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; +using ErrorEventArgs = Newtonsoft.Json.Serialization.ErrorEventArgs; namespace Pilz.Configuration { @@ -10,6 +12,7 @@ namespace Pilz.Configuration public event EventHandler AutoSavingSettings; public event EventHandler SavingSettings; public event EventHandler SavedSettings; + public event EventHandler OnSerializationError; protected ISettings defaultInstance = null; protected bool enableAutoSave = false; @@ -119,8 +122,21 @@ namespace Pilz.Configuration return new JsonSerializerSettings() { Formatting = Formatting.Indented, - TypeNameHandling = TypeNameHandling.Auto + TypeNameHandling = TypeNameHandling.Auto, + Error = JsonSerializer_OnError, }; } + + protected virtual void JsonSerializer_OnError(object sender, ErrorEventArgs e) + { + const string errorResolvingType = "Error resolving type specified in JSON"; + + // Invoke event + OnSerializationError?.Invoke(sender, e); + + // Handle ourself + if (!e.ErrorContext.Handled && e.ErrorContext.Error is JsonSerializationException serializationException && serializationException.Message.StartsWith(errorResolvingType)) + e.ErrorContext.Handled = true; + } } } \ No newline at end of file