Don't include namespace and assembly in exported image effect json

This commit is contained in:
Jaex 2020-05-15 01:57:55 +03:00
parent ee6d85cf1a
commit 5be913cb76
4 changed files with 67 additions and 0 deletions

View file

@ -25,6 +25,7 @@ You should have received a copy of the GNU General Public License
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using ShareX.HelpersLib.Properties;
using System;
using System.ComponentModel;
@ -52,6 +53,8 @@ public partial class ExportImportControl : UserControl
// Can't use generic class because not works in form designer
public Type ObjectType { get; set; }
public ISerializationBinder SerializationBinder { get; set; }
[DefaultValue(false)]
public bool ExportIgnoreDefaultValue { get; set; }
@ -87,6 +90,10 @@ public string Serialize(object obj)
serializer.DefaultValueHandling = ExportIgnoreDefaultValue ? DefaultValueHandling.Ignore : DefaultValueHandling.Include;
serializer.NullValueHandling = ExportIgnoreNull ? NullValueHandling.Ignore : NullValueHandling.Include;
serializer.TypeNameHandling = TypeNameHandling.Auto;
if (SerializationBinder != null)
{
serializer.SerializationBinder = SerializationBinder;
}
serializer.Serialize(textWriter, obj, ObjectType);
}
@ -172,6 +179,10 @@ public object Deserialize(string json)
serializer.Error += (sender, e) => e.ErrorContext.Handled = true;
serializer.ObjectCreationHandling = ObjectCreationHandling.Replace;
serializer.TypeNameHandling = TypeNameHandling.Auto;
if (SerializationBinder != null)
{
serializer.SerializationBinder = SerializationBinder;
}
return serializer.Deserialize(textReader, ObjectType);
}
}

View file

@ -283,6 +283,7 @@
<Compile Include="ShareXTheme.cs" />
<Compile Include="TextBoxTraceListener.cs" />
<Compile Include="SafeStringEnumConverter.cs" />
<Compile Include="TypeNameSerializationBinder.cs" />
<Compile Include="UITypeEditors\EnumDescriptionConverter.cs" />
<Compile Include="UITypeEditors\DirectoryNameEditor.cs" />
<Compile Include="UITypeEditors\GradientEditor.cs" />

View file

@ -0,0 +1,54 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2020 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.Serialization;
using System;
namespace ShareX.HelpersLib
{
public class TypeNameSerializationBinder : ISerializationBinder
{
public string AppNamespace { get; private set; }
public string AppAssembly { get; private set; }
public TypeNameSerializationBinder(string appNamespace, string appAssembly)
{
AppNamespace = appNamespace;
AppAssembly = appAssembly;
}
public void BindToName(Type serializedType, out string assemblyName, out string typeName)
{
assemblyName = null;
typeName = serializedType.Name;
}
public Type BindToType(string assemblyName, string typeName)
{
string resolvedTypeName = $"{AppNamespace}.{typeName}, {AppAssembly}";
return Type.GetType(resolvedTypeName, true);
}
}
}

View file

@ -65,6 +65,7 @@ public ImageEffectsForm(Bitmap bmp, List<ImageEffectPreset> presets, int selecte
SelectedPresetIndex = selectedPresetIndex;
eiImageEffects.ObjectType = typeof(ImageEffectPreset);
eiImageEffects.SerializationBinder = new TypeNameSerializationBinder("ShareX.ImageEffectsLib", "ShareX.ImageEffectsLib");
AddAllEffectsToContextMenu();
}