-Fixed deserializing to IEnumerable<T> properties

This commit is contained in:
JamesNK
2011-08-02 01:15:21 +00:00
parent a1f59aff82
commit 628e7977e8
2 changed files with 28 additions and 3 deletions
@@ -4684,5 +4684,21 @@ keyword such as type of business.""
PrivateMembersClassWithAttributes c2 = JsonConvert.DeserializeObject<PrivateMembersClassWithAttributes>(json);
Assert.AreEqual("readonlyString!", c2.UseValue());
}
public partial class BusRun
{
public IEnumerable<Nullable<DateTime>> Departures { get; set; }
public Boolean WheelchairAccessible { get; set; }
}
public void DeserializeGenericEnumerableProperty()
{
BusRun r = JsonConvert.DeserializeObject<BusRun>("{\"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));
}
}
}
@@ -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<object>(genericWrapperConstructor);