-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>(); StringComparison e5 = v.ToObject<StringComparison>();
Assert.AreEqual(StringComparison.Ordinal, e5); Assert.AreEqual(StringComparison.Ordinal, e5);
v = new JValue((int)StringComparison.InvariantCultureIgnoreCase); v = new JValue((int)StringComparison.OrdinalIgnoreCase);
Assert.AreEqual(JTokenType.Integer, v.Type); Assert.AreEqual(JTokenType.Integer, v.Type);
StringComparison e6 = v.ToObject<StringComparison>(); StringComparison e6 = v.ToObject<StringComparison>();
Assert.AreEqual(StringComparison.InvariantCultureIgnoreCase, e6); Assert.AreEqual(StringComparison.OrdinalIgnoreCase, e6);
} }
#endif #endif
} }
@@ -127,6 +127,17 @@ namespace Newtonsoft.Json.Tests.Serialization
} }
#endif #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 #if !NET20
[Test] [Test]
public void PopulateResetSettings() public void PopulateResetSettings()
@@ -101,7 +101,7 @@ namespace Newtonsoft.Json.Serialization
CreatedType = typeof(List<object>); CreatedType = typeof(List<object>);
if (CollectionItemType != null) if (CollectionItemType != null)
ParametrizedCreator = CollectionUtils.ResolveEnumableCollectionConstructor(underlyingType, CollectionItemType); ParametrizedCreator = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType);
IsReadOnlyOrFixedSize = ReflectionUtils.InheritsGenericDefinition(underlyingType, typeof(ReadOnlyCollection<>)); IsReadOnlyOrFixedSize = ReflectionUtils.InheritsGenericDefinition(underlyingType, typeof(ReadOnlyCollection<>));
canDeserialize = true; canDeserialize = true;
@@ -119,7 +119,7 @@ namespace Newtonsoft.Json.Serialization
CreatedType = typeof(HashSet<>).MakeGenericType(CollectionItemType); CreatedType = typeof(HashSet<>).MakeGenericType(CollectionItemType);
#endif #endif
ParametrizedCreator = CollectionUtils.ResolveEnumableCollectionConstructor(underlyingType, CollectionItemType); ParametrizedCreator = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType);
canDeserialize = true; canDeserialize = true;
ShouldCreateWrapper = true; ShouldCreateWrapper = true;
} }
@@ -133,7 +133,7 @@ namespace Newtonsoft.Json.Serialization
CreatedType = typeof(ReadOnlyCollection<>).MakeGenericType(CollectionItemType); CreatedType = typeof(ReadOnlyCollection<>).MakeGenericType(CollectionItemType);
_genericCollectionDefinitionType = typeof(List<>).MakeGenericType(CollectionItemType); _genericCollectionDefinitionType = typeof(List<>).MakeGenericType(CollectionItemType);
ParametrizedCreator = CollectionUtils.ResolveEnumableCollectionConstructor(CreatedType, CollectionItemType); ParametrizedCreator = CollectionUtils.ResolveEnumerableCollectionConstructor(CreatedType, CollectionItemType);
IsReadOnlyOrFixedSize = true; IsReadOnlyOrFixedSize = true;
canDeserialize = (ParametrizedCreator != null); canDeserialize = (ParametrizedCreator != null);
} }
@@ -145,7 +145,7 @@ namespace Newtonsoft.Json.Serialization
if (ReflectionUtils.IsGenericDefinition(UnderlyingType, typeof(IEnumerable<>))) if (ReflectionUtils.IsGenericDefinition(UnderlyingType, typeof(IEnumerable<>)))
CreatedType = typeof(List<>).MakeGenericType(CollectionItemType); CreatedType = typeof(List<>).MakeGenericType(CollectionItemType);
ParametrizedCreator = CollectionUtils.ResolveEnumableCollectionConstructor(underlyingType, CollectionItemType); ParametrizedCreator = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType);
#if !(NET35 || NET20 || NETFX_CORE) #if !(NET35 || NET20 || NETFX_CORE)
if (ParametrizedCreator == null && underlyingType.Name == FSharpUtils.FSharpListTypeName) if (ParametrizedCreator == null && underlyingType.Name == FSharpUtils.FSharpListTypeName)
@@ -117,7 +117,7 @@ namespace Newtonsoft.Json.Serialization
if (keyType != null && valueType != null) 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 !(NET35 || NET20 || NETFX_CORE)
if (ParametrizedCreator == null && underlyingType.Name == FSharpUtils.FSharpMapTypeName) if (ParametrizedCreator == null && underlyingType.Name == FSharpUtils.FSharpMapTypeName)
@@ -102,9 +102,10 @@ namespace Newtonsoft.Json.Utilities
return false; 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); Type genericEnumerable = typeof(IEnumerable<>).MakeGenericType(collectionItemType);
ConstructorInfo match = null;
foreach (ConstructorInfo constructor in collectionType.GetConstructors(BindingFlags.Public | BindingFlags.Instance)) foreach (ConstructorInfo constructor in collectionType.GetConstructors(BindingFlags.Public | BindingFlags.Instance))
{ {
@@ -112,11 +113,25 @@ namespace Newtonsoft.Json.Utilities
if (parameters.Count == 1) if (parameters.Count == 1)
{ {
if (genericEnumerable.IsAssignableFrom(parameters[0].ParameterType)) if (genericEnumerable == parameters[0].ParameterType)
return JsonTypeReflector.ReflectionDelegateFactory.CreateParametrizedConstructor(constructor); {
// 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; return null;
} }