From 245c418b9b1b17b71963b6cae4fe3f7f61ffa54a Mon Sep 17 00:00:00 2001 From: Robert Date: Thu, 30 Sep 2021 23:45:26 +0200 Subject: [PATCH] Fix numeric serialization --- src/Artemis.Core/Constants.cs | 4 +-- .../JsonConverters/NumericJsonConverter.cs | 25 +++++++++++++++++++ .../Controls/DataModelPicker.xaml.cs | 20 +++++++++------ 3 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 src/Artemis.Core/JsonConverters/NumericJsonConverter.cs diff --git a/src/Artemis.Core/Constants.cs b/src/Artemis.Core/Constants.cs index 7921af0bc..636fe1187 100644 --- a/src/Artemis.Core/Constants.cs +++ b/src/Artemis.Core/Constants.cs @@ -62,13 +62,13 @@ namespace Artemis.Core internal static JsonSerializerSettings JsonConvertSettings = new() { - Converters = new List {new SKColorConverter(), new ForgivingIntConverter()} + Converters = new List {new SKColorConverter(), new NumericJsonConverter(), new ForgivingIntConverter()} }; internal static JsonSerializerSettings JsonConvertTypedSettings = new() { TypeNameHandling = TypeNameHandling.All, - Converters = new List {new SKColorConverter(), new ForgivingIntConverter()} + Converters = new List {new SKColorConverter(), new NumericJsonConverter(), new ForgivingIntConverter()} }; /// diff --git a/src/Artemis.Core/JsonConverters/NumericJsonConverter.cs b/src/Artemis.Core/JsonConverters/NumericJsonConverter.cs new file mode 100644 index 000000000..e2ffd62a0 --- /dev/null +++ b/src/Artemis.Core/JsonConverters/NumericJsonConverter.cs @@ -0,0 +1,25 @@ +using System; +using Newtonsoft.Json; + +namespace Artemis.Core.JsonConverters +{ + internal class NumericJsonConverter : JsonConverter + { + #region Overrides of JsonConverter + + /// + public override void WriteJson(JsonWriter writer, Numeric value, JsonSerializer serializer) + { + float floatValue = value; + writer.WriteValue(floatValue); + } + + /// + public override Numeric ReadJson(JsonReader reader, Type objectType, Numeric existingValue, bool hasExistingValue, JsonSerializer serializer) + { + return new Numeric(reader.Value); + } + + #endregion + } +} \ No newline at end of file diff --git a/src/Artemis.UI.Shared/Controls/DataModelPicker.xaml.cs b/src/Artemis.UI.Shared/Controls/DataModelPicker.xaml.cs index dc8b45f2e..84d3b4d33 100644 --- a/src/Artemis.UI.Shared/Controls/DataModelPicker.xaml.cs +++ b/src/Artemis.UI.Shared/Controls/DataModelPicker.xaml.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; -using System.Runtime.CompilerServices; using System.Windows; using System.Windows.Controls; using System.Windows.Media; @@ -90,7 +89,7 @@ namespace Artemis.UI.Shared.Controls public DataModelPicker() { SelectPropertyCommand = new DelegateCommand(ExecuteSelectPropertyCommand); - Unloaded += (_, _) => DataModelViewModel?.Dispose(); + Unloaded += OnUnloaded; InitializeComponent(); GetDataModel(); UpdateValueDisplay(); @@ -206,11 +205,6 @@ namespace Artemis.UI.Shared.Controls DataModelPathSelected?.Invoke(this, e); } - protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) - { - PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); - } - private void GetDataModel() { ChangeDataModel(_dataModelUIService.GetPluginDataModelVisualization(Modules?.ToList() ?? new List(), true)); @@ -332,6 +326,18 @@ namespace Artemis.UI.Shared.Controls Dispatcher.Invoke(UpdateValueDisplay, DispatcherPriority.DataBind); } + private void OnUnloaded(object o, RoutedEventArgs routedEventArgs) + { + if (DataModelPath != null) + { + DataModelPath.PathInvalidated -= PathValidationChanged; + DataModelPath.PathValidated -= PathValidationChanged; + } + + DataModelViewModel?.Dispose(); + } + + /// public event PropertyChangedEventHandler? PropertyChanged; } } \ No newline at end of file