diff --git a/Src/Newtonsoft.Json.Tests/Issues/Issue0198.cs b/Src/Newtonsoft.Json.Tests/Issues/Issue0198.cs new file mode 100644 index 00000000..74b9632f --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Issues/Issue0198.cs @@ -0,0 +1,123 @@ +#region License +// Copyright (c) 2007 James Newton-King +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +#endregion + +#if !(NET20 || NET35 || PORTABLE || PORTABLE40) +using System; +using System.Collections; +using System.Collections.Generic; +using System.Dynamic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Runtime.Serialization; +using Newtonsoft.Json.Linq; +using Newtonsoft.Json.Serialization; +#if DNXCORE50 +using Xunit; +using Test = Xunit.FactAttribute; +using Assert = Newtonsoft.Json.Tests.XUnitAssert; +#else +using NUnit.Framework; +#endif + +namespace Newtonsoft.Json.Tests.Issues +{ + [TestFixture] + public class Issue0198 : TestFixtureBase + { + [Test] + public void Test() + { + IEnumerable objects = new List + { + new TestClass1 + { + Prop1 = new HashSet + { + new TestClass2 + { + MyProperty1 = "Test1", + MyProperty2 = "Test2", + } + }, + Prop2 = new List + { + "Test1", + "Test1" + }, + Prop3 = new HashSet + { + new TestClass2 + { + MyProperty1 = "Test1", + MyProperty2 = "Test2", + } + }, + } + }; + + string serializedData = JsonConvert.SerializeObject(objects, new JsonSerializerSettings + { + TypeNameHandling = TypeNameHandling.All, + Formatting = Formatting.Indented + }); + + IEnumerable a = JsonConvert.DeserializeObject>(serializedData, new JsonSerializerSettings + { + TypeNameHandling = TypeNameHandling.All + }); + + TestClass1 o = a.First(); + + Assert.AreEqual(1, o.Prop1.Count); + Assert.AreEqual(1, o.Prop2.Count); + Assert.AreEqual(1, o.Prop3.Count); + } + + class TestClass1 : AbstactClass + { + public TestClass1() + { + Prop1 = new HashSet(); + Prop2 = new HashSet(); + } + + public ICollection Prop1 { get; set; } + public ICollection Prop2 { get; set; } + } + + class TestClass2 + { + public string MyProperty1 { get; set; } + public string MyProperty2 { get; set; } + } + + abstract class AbstactClass + { + public ICollection Prop3 { get; set; } = new List(); + } + } +} +#endif \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index cb86bf5a..39b2e585 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -912,7 +912,7 @@ namespace Newtonsoft.Json.Serialization throw JsonSerializationException.Create(reader, "Cannot populate list type {0}.".FormatWith(CultureInfo.InvariantCulture, contract.CreatedType)); } - value = PopulateList((arrayContract.ShouldCreateWrapper) ? arrayContract.CreateWrapper(existingValue) : (IList)existingValue, reader, arrayContract, member, id); + value = PopulateList((arrayContract.ShouldCreateWrapper || !(existingValue is IList)) ? arrayContract.CreateWrapper(existingValue) : (IList)existingValue, reader, arrayContract, member, id); } return value;