From 628e7977e81939eef5b30316e3aadeb9f67e1646 Mon Sep 17 00:00:00 2001 From: JamesNK Date: Tue, 2 Aug 2011 01:15:21 +0000 Subject: [PATCH] -Fixed deserializing to IEnumerable properties --- .../Serialization/JsonSerializerTest.cs | 16 ++++++++++++++++ .../Serialization/JsonArrayContract.cs | 15 ++++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs index 83ed3f26..9eb401c7 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs @@ -4684,5 +4684,21 @@ keyword such as type of business."" PrivateMembersClassWithAttributes c2 = JsonConvert.DeserializeObject(json); Assert.AreEqual("readonlyString!", c2.UseValue()); } + + public partial class BusRun + { + public IEnumerable> Departures { get; set; } + public Boolean WheelchairAccessible { get; set; } + } + + public void DeserializeGenericEnumerableProperty() + { + BusRun r = JsonConvert.DeserializeObject("{\"Departures\":[\"\\/Date(1309874148734-0400)\\/\",\"\\/Date(1309874148739-0400)\\/\",null],\"WheelchairAccessible\":true}"); + + Assert.AreEqual(3, r.Departures.Count()); + Assert.IsNotNull(r.Departures.ElementAt(0)); + Assert.IsNotNull(r.Departures.ElementAt(1)); + Assert.IsNull(r.Departures.ElementAt(2)); + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs b/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs index 01aede8e..34266d4a 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs @@ -55,6 +55,11 @@ namespace Newtonsoft.Json.Serialization { CollectionItemType = _genericCollectionDefinitionType.GetGenericArguments()[0]; } + else if (underlyingType.IsGenericType && underlyingType.GetGenericTypeDefinition() == typeof(IEnumerable<>)) + { + _genericCollectionDefinitionType = typeof (IEnumerable<>); + CollectionItemType = underlyingType.GetGenericArguments()[0]; + } else { CollectionItemType = ReflectionUtils.GetCollectionItemType(UnderlyingType); @@ -105,9 +110,13 @@ namespace Newtonsoft.Json.Serialization { _genericWrapperType = ReflectionUtils.MakeGenericType(typeof (CollectionWrapper<>), CollectionItemType); - Type constructorArgument = (ReflectionUtils.InheritsGenericDefinition(_genericCollectionDefinitionType, typeof (List<>))) - ? ReflectionUtils.MakeGenericType(typeof (ICollection<>), CollectionItemType) - : _genericCollectionDefinitionType; + Type constructorArgument; + + if (ReflectionUtils.InheritsGenericDefinition(_genericCollectionDefinitionType, typeof(List<>)) + || _genericCollectionDefinitionType.GetGenericTypeDefinition() == typeof(IEnumerable<>)) + constructorArgument = ReflectionUtils.MakeGenericType(typeof(ICollection<>), CollectionItemType); + else + constructorArgument = _genericCollectionDefinitionType; ConstructorInfo genericWrapperConstructor = _genericWrapperType.GetConstructor(new[] { constructorArgument }); _genericWrapperCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall(genericWrapperConstructor);