Skip to content

Commit

Permalink
Added JsonSerializer instead of BinaryFormatter.
Browse files Browse the repository at this point in the history
  • Loading branch information
GintasS committed Jul 7, 2021
1 parent 06a9deb commit c5bbb31
Showing 1 changed file with 3 additions and 19 deletions.
22 changes: 3 additions & 19 deletions GitCommands/CommitTemplateItem.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Drawing;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using GitCommands.Utils;

namespace GitCommands
Expand Down Expand Up @@ -90,10 +88,9 @@ private static string SerializeCommitTemplates(CommitTemplateItem[]? items)

byte[] memoryData = Convert.FromBase64String(serializedString.Substring(p + 1));
using MemoryStream rs = new(memoryData, 0, length);
#pragma warning disable SYSLIB0011 // Type or member is obsolete
BinaryFormatter sf = new() { Binder = new MoveNamespaceDeserializationBinder() };
commitTemplateItem = (CommitTemplateItem[])sf.Deserialize(rs);
#pragma warning restore SYSLIB0011 // Type or member is obsolete

var stringFromMemoryStream = Encoding.UTF8.GetString(rs.ToArray());
commitTemplateItem = JsonSerializer.Deserialize<CommitTemplateItem[]>(stringFromMemoryStream);

shouldBeUpdated = true;
}
Expand All @@ -106,17 +103,4 @@ private static string SerializeCommitTemplates(CommitTemplateItem[]? items)
return commitTemplateItem;
}
}

public sealed class MoveNamespaceDeserializationBinder : SerializationBinder
{
private const string OldNamespace = "GitUI.CommandsDialogs.CommitDialog";
private const string NewNamespace = "GitCommands";

public override Type? BindToType(string assemblyName, string typeName)
{
typeName = typeName.Replace(OldNamespace, NewNamespace);

return Type.GetType($"{typeName}, {assemblyName}");
}
}
}

0 comments on commit c5bbb31

Please sign in to comment.