From ae7318cc8e30205ee10c7410b5f9266fdc766a99 Mon Sep 17 00:00:00 2001 From: Ionite Date: Wed, 28 Jun 2023 02:14:48 -0400 Subject: [PATCH] Fix valid json detection --- .../ViewModels/SelectInstallLocationsViewModel.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/StabilityMatrix/ViewModels/SelectInstallLocationsViewModel.cs b/StabilityMatrix/ViewModels/SelectInstallLocationsViewModel.cs index 5ff07dfd..5887bd03 100644 --- a/StabilityMatrix/ViewModels/SelectInstallLocationsViewModel.cs +++ b/StabilityMatrix/ViewModels/SelectInstallLocationsViewModel.cs @@ -2,10 +2,12 @@ using System.IO; using System.Linq; using System.Text.Json; +using System.Text.Json.Serialization; using System.Threading.Tasks; using AsyncAwaitBestPractices; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; +using NLog; using Ookii.Dialogs.Wpf; using StabilityMatrix.Helper; using StabilityMatrix.Models; @@ -14,6 +16,8 @@ namespace StabilityMatrix.ViewModels; public partial class SelectInstallLocationsViewModel : ObservableObject { + private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); + private const string ValidExistingDirectoryText = "Valid existing data directory found"; private const string InvalidDirectoryText = "Directory must be empty or have a valid settings.json file"; @@ -75,15 +79,19 @@ public partial class SelectInstallLocationsViewModel : ObservableObject try { var jsonText = await File.ReadAllTextAsync(settingsPath); - var _ = JsonSerializer.Deserialize(jsonText); + var _ = JsonSerializer.Deserialize(jsonText, new JsonSerializerOptions + { + Converters = { new JsonStringEnumConverter() } + }); // If successful, show existing badge IsStatusBadgeVisible = true; IsDirectoryValid = true; DirectoryStatusText = ValidExistingDirectoryText; return true; } - catch (Exception) + catch (Exception e) { + Logger.Info("Failed to deserialize settings.json: {Msg}", e.Message); // If not, show error badge, and set directory to invalid to prevent continuing IsStatusBadgeVisible = true; IsDirectoryValid = false;