This commit is contained in:
James Newton-King
2019-03-30 17:01:39 +13:00
parent 05d721e36f
commit 793c9a0a3d
@@ -115,7 +115,6 @@ namespace Newtonsoft.Json.Serialization
public JsonArrayContract(Type underlyingType) public JsonArrayContract(Type underlyingType)
: base(underlyingType) : base(underlyingType)
{ {
var t = ReflectionUtils.EnsureNotByRefType(ReflectionUtils.EnsureNotNullableType(underlyingType));
ContractType = JsonContractType.Array; ContractType = JsonContractType.Array;
IsArray = CreatedType.IsArray; IsArray = CreatedType.IsArray;
@@ -131,58 +130,58 @@ namespace Newtonsoft.Json.Serialization
canDeserialize = true; canDeserialize = true;
IsMultidimensionalArray = (IsArray && UnderlyingType.GetArrayRank() > 1); IsMultidimensionalArray = (IsArray && UnderlyingType.GetArrayRank() > 1);
} }
else if (typeof(IList).IsAssignableFrom(t)) else if (typeof(IList).IsAssignableFrom(NonNullableUnderlyingType))
{ {
if (ReflectionUtils.ImplementsGenericDefinition(t, typeof(ICollection<>), out _genericCollectionDefinitionType)) if (ReflectionUtils.ImplementsGenericDefinition(NonNullableUnderlyingType, typeof(ICollection<>), out _genericCollectionDefinitionType))
{ {
CollectionItemType = _genericCollectionDefinitionType.GetGenericArguments()[0]; CollectionItemType = _genericCollectionDefinitionType.GetGenericArguments()[0];
} }
else else
{ {
CollectionItemType = ReflectionUtils.GetCollectionItemType(t); CollectionItemType = ReflectionUtils.GetCollectionItemType(NonNullableUnderlyingType);
} }
if (t == typeof(IList)) if (NonNullableUnderlyingType == typeof(IList))
{ {
CreatedType = typeof(List<object>); CreatedType = typeof(List<object>);
} }
if (CollectionItemType != null) if (CollectionItemType != null)
{ {
_parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(t, CollectionItemType); _parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(NonNullableUnderlyingType, CollectionItemType);
} }
IsReadOnlyOrFixedSize = ReflectionUtils.InheritsGenericDefinition(t, typeof(ReadOnlyCollection<>)); IsReadOnlyOrFixedSize = ReflectionUtils.InheritsGenericDefinition(NonNullableUnderlyingType, typeof(ReadOnlyCollection<>));
canDeserialize = true; canDeserialize = true;
} }
else if (ReflectionUtils.ImplementsGenericDefinition(t, typeof(ICollection<>), out _genericCollectionDefinitionType)) else if (ReflectionUtils.ImplementsGenericDefinition(NonNullableUnderlyingType, typeof(ICollection<>), out _genericCollectionDefinitionType))
{ {
CollectionItemType = _genericCollectionDefinitionType.GetGenericArguments()[0]; CollectionItemType = _genericCollectionDefinitionType.GetGenericArguments()[0];
if (ReflectionUtils.IsGenericDefinition(t, typeof(ICollection<>)) if (ReflectionUtils.IsGenericDefinition(NonNullableUnderlyingType, typeof(ICollection<>))
|| ReflectionUtils.IsGenericDefinition(t, typeof(IList<>))) || ReflectionUtils.IsGenericDefinition(NonNullableUnderlyingType, typeof(IList<>)))
{ {
CreatedType = typeof(List<>).MakeGenericType(CollectionItemType); CreatedType = typeof(List<>).MakeGenericType(CollectionItemType);
} }
#if HAVE_ISET #if HAVE_ISET
if (ReflectionUtils.IsGenericDefinition(t, typeof(ISet<>))) if (ReflectionUtils.IsGenericDefinition(NonNullableUnderlyingType, typeof(ISet<>)))
{ {
CreatedType = typeof(HashSet<>).MakeGenericType(CollectionItemType); CreatedType = typeof(HashSet<>).MakeGenericType(CollectionItemType);
} }
#endif #endif
_parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(t, CollectionItemType); _parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(NonNullableUnderlyingType, CollectionItemType);
canDeserialize = true; canDeserialize = true;
ShouldCreateWrapper = true; ShouldCreateWrapper = true;
} }
#if HAVE_READ_ONLY_COLLECTIONS #if HAVE_READ_ONLY_COLLECTIONS
else if (ReflectionUtils.ImplementsGenericDefinition(t, typeof(IReadOnlyCollection<>), out tempCollectionType)) else if (ReflectionUtils.ImplementsGenericDefinition(NonNullableUnderlyingType, typeof(IReadOnlyCollection<>), out tempCollectionType))
{ {
CollectionItemType = tempCollectionType.GetGenericArguments()[0]; CollectionItemType = tempCollectionType.GetGenericArguments()[0];
if (ReflectionUtils.IsGenericDefinition(t, typeof(IReadOnlyCollection<>)) if (ReflectionUtils.IsGenericDefinition(NonNullableUnderlyingType, typeof(IReadOnlyCollection<>))
|| ReflectionUtils.IsGenericDefinition(t, typeof(IReadOnlyList<>))) || ReflectionUtils.IsGenericDefinition(NonNullableUnderlyingType, typeof(IReadOnlyList<>)))
{ {
CreatedType = typeof(ReadOnlyCollection<>).MakeGenericType(CollectionItemType); CreatedType = typeof(ReadOnlyCollection<>).MakeGenericType(CollectionItemType);
} }
@@ -191,14 +190,14 @@ namespace Newtonsoft.Json.Serialization
_parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(CreatedType, CollectionItemType); _parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(CreatedType, CollectionItemType);
#if HAVE_FSHARP_TYPES #if HAVE_FSHARP_TYPES
StoreFSharpListCreatorIfNecessary(t); StoreFSharpListCreatorIfNecessary(NonNullableUnderlyingType);
#endif #endif
IsReadOnlyOrFixedSize = true; IsReadOnlyOrFixedSize = true;
canDeserialize = HasParameterizedCreatorInternal; canDeserialize = HasParameterizedCreatorInternal;
} }
#endif #endif
else if (ReflectionUtils.ImplementsGenericDefinition(t, typeof(IEnumerable<>), out tempCollectionType)) else if (ReflectionUtils.ImplementsGenericDefinition(NonNullableUnderlyingType, typeof(IEnumerable<>), out tempCollectionType))
{ {
CollectionItemType = tempCollectionType.GetGenericArguments()[0]; CollectionItemType = tempCollectionType.GetGenericArguments()[0];
@@ -207,13 +206,13 @@ namespace Newtonsoft.Json.Serialization
CreatedType = typeof(List<>).MakeGenericType(CollectionItemType); CreatedType = typeof(List<>).MakeGenericType(CollectionItemType);
} }
_parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(t, CollectionItemType); _parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(NonNullableUnderlyingType, CollectionItemType);
#if HAVE_FSHARP_TYPES #if HAVE_FSHARP_TYPES
StoreFSharpListCreatorIfNecessary(t); StoreFSharpListCreatorIfNecessary(NonNullableUnderlyingType);
#endif #endif
if (t.IsGenericType() && t.GetGenericTypeDefinition() == typeof(IEnumerable<>)) if (NonNullableUnderlyingType.IsGenericType() && NonNullableUnderlyingType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
{ {
_genericCollectionDefinitionType = tempCollectionType; _genericCollectionDefinitionType = tempCollectionType;
@@ -253,7 +252,7 @@ namespace Newtonsoft.Json.Serialization
#endif #endif
if (ImmutableCollectionsUtils.TryBuildImmutableForArrayContract( if (ImmutableCollectionsUtils.TryBuildImmutableForArrayContract(
t, NonNullableUnderlyingType,
CollectionItemType, CollectionItemType,
out Type immutableCreatedType, out Type immutableCreatedType,
out ObjectConstructor<object> immutableParameterizedCreator)) out ObjectConstructor<object> immutableParameterizedCreator))