Skip to content

Commit b6b9325

Browse files
committed
fix: Deserialize notification JSON in more compatible way
1 parent ab10d1a commit b6b9325

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

Source/Menu/Notifications/NotificationManager.cs

+23-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// COPYRIGHT 2009 - 2024 by the Open Rails project.
1+
// COPYRIGHT 2009 - 2024 by the Open Rails project.
22
//
33
// This file is part of Open Rails.
44
//
@@ -22,7 +22,6 @@
2222
using System.Linq;
2323
using System.Net;
2424
using System.Resources;
25-
using System.Runtime.InteropServices;
2625
using System.Text;
2726
using System.Windows;
2827
using System.Windows.Forms;
@@ -32,6 +31,7 @@
3231
using ORTS.Updater;
3332
using static ORTS.Common.SystemInfo;
3433
using static Menu.Notifications.NotificationPage;
34+
using Newtonsoft.Json.Serialization;
3535

3636
// Behaviour
3737
// Notifications are read only once as a background task at start into Notifications.
@@ -154,7 +154,7 @@ public Notifications GetNotifications()
154154
notificationsSerial = GetRemoteJson();
155155
}
156156

157-
var jsonSettings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto };
157+
var jsonSettings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto, SerializationBinder = new NotificationSerializationBinder() };
158158
var jsonInput = JsonConvert.DeserializeObject<Notifications>(notificationsSerial, jsonSettings);
159159

160160
NewPages.Count = 0;
@@ -626,7 +626,7 @@ public OverrideParameterList GetOverrideParameters()
626626
// Input from local file into a string
627627
var overrideParametersSerial = File.ReadAllText(filename);
628628

629-
var jsonSettings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto };
629+
var jsonSettings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto, SerializationBinder = new NotificationSerializationBinder() };
630630
var jsonInput = JsonConvert.DeserializeObject<OverrideParameterList>(overrideParametersSerial, jsonSettings);
631631

632632
return jsonInput;
@@ -690,5 +690,24 @@ public void AppendToLog(string record)
690690
using (StreamWriter sw = File.AppendText(LogFile)) sw.WriteLine(record);
691691
}
692692
#endregion
693+
694+
class NotificationSerializationBinder : ISerializationBinder
695+
{
696+
public void BindToName(Type serializedType, out string assemblyName, out string typeName)
697+
{
698+
throw new NotImplementedException();
699+
}
700+
701+
public Type BindToType(string assemblyName, string typeName)
702+
{
703+
if (assemblyName == "Menu")
704+
{
705+
var ns = typeof(Notifications).Namespace;
706+
var name = typeName.Split('.').Last();
707+
return typeof(Notifications).Assembly.GetType($"{ns}.{name}");
708+
}
709+
return null;
710+
}
711+
}
693712
}
694713
}

0 commit comments

Comments
 (0)