From 727e109edad4a4c4d46e7dc312aad6b358aa5d89 Mon Sep 17 00:00:00 2001 From: Justin Van Patten Date: Wed, 21 Jun 2017 15:31:51 -0700 Subject: [PATCH 1/2] Fixed deserializing ObservableCollection in .NET Core 2.0 (#1353) --- .../Serialization/JsonSerializerTest.cs | 14 ++++++++++++++ .../Serialization/DefaultContractResolver.cs | 7 ++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs index 35a00c5b..401bf60c 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs @@ -1085,6 +1085,20 @@ namespace Newtonsoft.Json.Tests.Serialization Assert.AreEqual("1", s[0]); Assert.AreEqual("2", s[1]); } + + [Test] + public void SerializeObservableCollection() + { + ObservableCollection c1 = new ObservableCollection { "1", "2" }; + + string output = JsonConvert.SerializeObject(c1); + Assert.AreEqual("[\"1\",\"2\"]", output); + + ObservableCollection c2 = JsonConvert.DeserializeObject>(output); + Assert.AreEqual(2, c2.Count); + Assert.AreEqual("1", c2[0]); + Assert.AreEqual("2", c2[1]); + } #endif [Test] diff --git a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs index d5e840f3..4827a94a 100644 --- a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs +++ b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs @@ -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; } From 17a34878b5f83917b55ab713298298d46d8e9cbd Mon Sep 17 00:00:00 2001 From: Bernard Vander Beken Date: Thu, 22 Jun 2017 23:10:15 +0200 Subject: [PATCH 2/2] Fix typo (#1354) --- Doc/doc.content | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/doc.content b/Doc/doc.content index a146c946..991d7d78 100644 --- a/Doc/doc.content +++ b/Doc/doc.content @@ -92,7 +92,7 @@ - + @@ -164,4 +164,4 @@ - \ No newline at end of file +