Fixed deserializing ObservableCollection in .NET Core 2.0 (#1353)

This commit is contained in:
Justin Van Patten
2017-06-22 10:31:51 +12:00
committed by James Newton-King
parent b529abba7c
commit 727e109eda
2 changed files with 18 additions and 3 deletions
@@ -1085,6 +1085,20 @@ namespace Newtonsoft.Json.Tests.Serialization
Assert.AreEqual("1", s[0]);
Assert.AreEqual("2", s[1]);
}
[Test]
public void SerializeObservableCollection()
{
ObservableCollection<string> c1 = new ObservableCollection<string> { "1", "2" };
string output = JsonConvert.SerializeObject(c1);
Assert.AreEqual("[\"1\",\"2\"]", output);
ObservableCollection<string> c2 = JsonConvert.DeserializeObject<ObservableCollection<string>>(output);
Assert.AreEqual(2, c2.Count);
Assert.AreEqual("1", c2[0]);
Assert.AreEqual("2", c2[1]);
}
#endif
[Test]
@@ -881,7 +881,7 @@ namespace Newtonsoft.Json.Serialization
}
}
private static bool IsConcurrentCollection(Type t)
private static bool IsConcurrentOrObservableCollection(Type t)
{
if (t.IsGenericType())
{
@@ -893,6 +893,7 @@ namespace Newtonsoft.Json.Serialization
case "System.Collections.Concurrent.ConcurrentStack`1":
case "System.Collections.Concurrent.ConcurrentBag`1":
case "System.Collections.Concurrent.ConcurrentDictionary`2":
case "System.Collections.ObjectModel.ObservableCollection`1":
return true;
}
}
@@ -903,7 +904,7 @@ namespace Newtonsoft.Json.Serialization
private static bool ShouldSkipDeserialized(Type t)
{
// ConcurrentDictionary throws an error in its OnDeserialized so ignore - http://json.codeplex.com/discussions/257093
if (IsConcurrentCollection(t))
if (IsConcurrentOrObservableCollection(t))
{
return true;
}
@@ -920,7 +921,7 @@ namespace Newtonsoft.Json.Serialization
private static bool ShouldSkipSerializing(Type t)
{
if (IsConcurrentCollection(t))
if (IsConcurrentOrObservableCollection(t))
{
return true;
}