-Fixed bug when deserializing into existing non-IDictionary collection

This commit is contained in:
James Newton-King
2018-02-12 19:47:21 +13:00
parent 39f2db6f2d
commit ce7f283b94
2 changed files with 36 additions and 2 deletions
+35 -1
View File
@@ -34,6 +34,7 @@ using System.Reflection;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Tests.TestObjects;
#if DNXCORE50 #if DNXCORE50
using Xunit; using Xunit;
using Test = Xunit.FactAttribute; using Test = Xunit.FactAttribute;
@@ -48,7 +49,7 @@ namespace Newtonsoft.Json.Tests.Issues
public class Issue0198 : TestFixtureBase public class Issue0198 : TestFixtureBase
{ {
[Test] [Test]
public void Test() public void Test_List()
{ {
IEnumerable<TestClass1> objects = new List<TestClass1> IEnumerable<TestClass1> objects = new List<TestClass1>
{ {
@@ -96,6 +97,29 @@ namespace Newtonsoft.Json.Tests.Issues
Assert.AreEqual(1, o.Prop3.Count); Assert.AreEqual(1, o.Prop3.Count);
} }
[Test]
public void Test_Collection()
{
TestClass3 c = new TestClass3();
c.Prop1 = new Dictionary<string, string>
{
["key"] = "value"
};
string serializedData = JsonConvert.SerializeObject(c, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All,
Formatting = Formatting.Indented
});
TestClass3 a = JsonConvert.DeserializeObject<TestClass3>(serializedData, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
});
Assert.AreEqual(1, a.Prop1.Count);
}
class TestClass1 : AbstactClass class TestClass1 : AbstactClass
{ {
public TestClass1() public TestClass1()
@@ -118,6 +142,16 @@ namespace Newtonsoft.Json.Tests.Issues
{ {
public ICollection<TestClass2> Prop3 { get; set; } = new List<TestClass2>(); public ICollection<TestClass2> Prop3 { get; set; } = new List<TestClass2>();
} }
class TestClass3
{
public TestClass3()
{
Prop1 = new ModelStateDictionary<string>();
}
public IDictionary<string, string> Prop1 { get; set; }
}
} }
} }
#endif #endif
@@ -557,7 +557,7 @@ namespace Newtonsoft.Json.Serialization
} }
else else
{ {
targetDictionary = PopulateDictionary(dictionaryContract.ShouldCreateWrapper ? dictionaryContract.CreateWrapper(existingValue) : (IDictionary)existingValue, reader, dictionaryContract, member, id); targetDictionary = PopulateDictionary(dictionaryContract.ShouldCreateWrapper || !(existingValue is IDictionary) ? dictionaryContract.CreateWrapper(existingValue) : (IDictionary)existingValue, reader, dictionaryContract, member, id);
} }
return targetDictionary; return targetDictionary;