-Fixed Win8 build

-Fixed serializing ObservableCollection<T> on Win8
This commit is contained in:
James Newton-King
2014-08-30 14:34:16 +12:00
parent 57ccaf00bf
commit 695742563b
5 changed files with 36 additions and 10 deletions
@@ -791,10 +791,10 @@ namespace Newtonsoft.Json.Tests.Linq
StringComparison e5 = v.ToObject<StringComparison>();
Assert.AreEqual(StringComparison.Ordinal, e5);
v = new JValue((int)StringComparison.InvariantCultureIgnoreCase);
v = new JValue((int)StringComparison.OrdinalIgnoreCase);
Assert.AreEqual(JTokenType.Integer, v.Type);
StringComparison e6 = v.ToObject<StringComparison>();
Assert.AreEqual(StringComparison.InvariantCultureIgnoreCase, e6);
Assert.AreEqual(StringComparison.OrdinalIgnoreCase, e6);
}
#endif
}
@@ -127,6 +127,17 @@ namespace Newtonsoft.Json.Tests.Serialization
}
#endif
#if !NET20
[Test]
public void DeserializeObservableCollection()
{
ObservableCollection<string> s = JsonConvert.DeserializeObject<ObservableCollection<string>>("['1','2']");
Assert.AreEqual(2, s.Count);
Assert.AreEqual("1", s[0]);
Assert.AreEqual("2", s[1]);
}
#endif
#if !NET20
[Test]
public void PopulateResetSettings()
@@ -101,7 +101,7 @@ namespace Newtonsoft.Json.Serialization
CreatedType = typeof(List<object>);
if (CollectionItemType != null)
ParametrizedCreator = CollectionUtils.ResolveEnumableCollectionConstructor(underlyingType, CollectionItemType);
ParametrizedCreator = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType);
IsReadOnlyOrFixedSize = ReflectionUtils.InheritsGenericDefinition(underlyingType, typeof(ReadOnlyCollection<>));
canDeserialize = true;
@@ -119,7 +119,7 @@ namespace Newtonsoft.Json.Serialization
CreatedType = typeof(HashSet<>).MakeGenericType(CollectionItemType);
#endif
ParametrizedCreator = CollectionUtils.ResolveEnumableCollectionConstructor(underlyingType, CollectionItemType);
ParametrizedCreator = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType);
canDeserialize = true;
ShouldCreateWrapper = true;
}
@@ -133,7 +133,7 @@ namespace Newtonsoft.Json.Serialization
CreatedType = typeof(ReadOnlyCollection<>).MakeGenericType(CollectionItemType);
_genericCollectionDefinitionType = typeof(List<>).MakeGenericType(CollectionItemType);
ParametrizedCreator = CollectionUtils.ResolveEnumableCollectionConstructor(CreatedType, CollectionItemType);
ParametrizedCreator = CollectionUtils.ResolveEnumerableCollectionConstructor(CreatedType, CollectionItemType);
IsReadOnlyOrFixedSize = true;
canDeserialize = (ParametrizedCreator != null);
}
@@ -145,7 +145,7 @@ namespace Newtonsoft.Json.Serialization
if (ReflectionUtils.IsGenericDefinition(UnderlyingType, typeof(IEnumerable<>)))
CreatedType = typeof(List<>).MakeGenericType(CollectionItemType);
ParametrizedCreator = CollectionUtils.ResolveEnumableCollectionConstructor(underlyingType, CollectionItemType);
ParametrizedCreator = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType);
#if !(NET35 || NET20 || NETFX_CORE)
if (ParametrizedCreator == null && underlyingType.Name == FSharpUtils.FSharpListTypeName)
@@ -117,7 +117,7 @@ namespace Newtonsoft.Json.Serialization
if (keyType != null && valueType != null)
{
ParametrizedCreator = CollectionUtils.ResolveEnumableCollectionConstructor(CreatedType, typeof(KeyValuePair<,>).MakeGenericType(keyType, valueType));
ParametrizedCreator = CollectionUtils.ResolveEnumerableCollectionConstructor(CreatedType, typeof(KeyValuePair<,>).MakeGenericType(keyType, valueType));
#if !(NET35 || NET20 || NETFX_CORE)
if (ParametrizedCreator == null && underlyingType.Name == FSharpUtils.FSharpMapTypeName)
@@ -102,9 +102,10 @@ namespace Newtonsoft.Json.Utilities
return false;
}
public static ObjectConstructor<object> ResolveEnumableCollectionConstructor(Type collectionType, Type collectionItemType)
public static ObjectConstructor<object> ResolveEnumerableCollectionConstructor(Type collectionType, Type collectionItemType)
{
Type genericEnumerable = typeof(IEnumerable<>).MakeGenericType(collectionItemType);
ConstructorInfo match = null;
foreach (ConstructorInfo constructor in collectionType.GetConstructors(BindingFlags.Public | BindingFlags.Instance))
{
@@ -112,11 +113,25 @@ namespace Newtonsoft.Json.Utilities
if (parameters.Count == 1)
{
if (genericEnumerable.IsAssignableFrom(parameters[0].ParameterType))
return JsonTypeReflector.ReflectionDelegateFactory.CreateParametrizedConstructor(constructor);
if (genericEnumerable == parameters[0].ParameterType)
{
// exact match
match = constructor;
break;
}
// incase we can't find an exact match, use first inexact
if (match == null)
{
if (genericEnumerable.IsAssignableFrom(parameters[0].ParameterType))
match = constructor;
}
}
}
if (match != null)
return JsonTypeReflector.ReflectionDelegateFactory.CreateParametrizedConstructor(match);
return null;
}