Handle missing enum values while deserializing json

This commit is contained in:
Jaex 2019-08-25 04:50:41 +03:00
parent 2c8a80e18c
commit 67b728fcce
6 changed files with 52 additions and 5 deletions

View file

@ -0,0 +1,46 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2019 ShareX Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
namespace ShareX.HelpersLib
{
public class SafeStringEnumConverter : StringEnumConverter
{
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
try
{
return base.ReadJson(reader, objectType, existingValue, serializer);
}
catch (JsonSerializationException)
{
return existingValue;
}
}
}
}

View file

@ -215,7 +215,7 @@ private static T LoadInternal(string filePath, string backupFolder = null)
using (JsonTextReader jsonReader = new JsonTextReader(streamReader))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Converters.Add(new StringEnumConverter());
serializer.Converters.Add(new SafeStringEnumConverter());
serializer.DateTimeZoneHandling = DateTimeZoneHandling.Local;
serializer.ObjectCreationHandling = ObjectCreationHandling.Replace;
settings = serializer.Deserialize<T>(jsonReader);

View file

@ -272,6 +272,7 @@
</Compile>
<Compile Include="NameParser\CodeMenuEntryFilename.cs" />
<Compile Include="TextBoxTraceListener.cs" />
<Compile Include="SafeStringEnumConverter.cs" />
<Compile Include="UITypeEditors\EnumDescriptionConverter.cs" />
<Compile Include="UITypeEditors\DirectoryNameEditor.cs" />
<Compile Include="UITypeEditors\GradientEditor.cs" />

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" xmlns:uap2="http://schemas.microsoft.com/appx/manifest/uap/windows10/2" xmlns:uap3="http://schemas.microsoft.com/appx/manifest/uap/windows10/3" xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10" xmlns:desktop2="http://schemas.microsoft.com/appx/manifest/desktop/windows10/2" IgnorableNamespaces="desktop2">
<Identity Name="19568ShareX.ShareX" ProcessorArchitecture="x64" Publisher="CN=366A5DE5-2EC7-43FD-B559-05986578C4CC" Version="13.0.0.0" />
<Identity Name="19568ShareX.ShareX" ProcessorArchitecture="x64" Publisher="CN=366A5DE5-2EC7-43FD-B559-05986578C4CC" Version="13.0.1.0" />
<Properties>
<DisplayName>ShareX</DisplayName>
<PublisherDisplayName>ShareX Team</PublisherDisplayName>

View file

@ -78,7 +78,7 @@ public static string VersionText
public static string TitleLong => $"{Title} ({Build})";
public static bool Dev { get; } = false;
public static bool Dev { get; } = true;
public static bool MultiInstance { get; private set; }
public static bool Portable { get; private set; }
public static bool PortableApps { get; private set; }

View file

@ -30,5 +30,5 @@
[assembly: AssemblyProduct("ShareX")]
[assembly: AssemblyCopyright("Copyright (c) 2007-2019 ShareX Team")]
[assembly: ComVisible(false)]
[assembly: AssemblyVersion("13.0.0")]
[assembly: AssemblyFileVersion("13.0.0")]
[assembly: AssemblyVersion("13.0.1")]
[assembly: AssemblyFileVersion("13.0.1")]