JsonArrayContract
Contract details for a Type used by the JsonSerializer.
using Newtonsoft.Json.Utilities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reflection;
namespace Newtonsoft.Json.Serialization
{
public class JsonArrayContract : JsonContainerContract
{
private readonly bool _isCollectionItemTypeNullableType;
private readonly Type _genericCollectionDefinitionType;
private Type _genericWrapperType;
private MethodCall<object, object> _genericWrapperCreator;
private Func<object> _genericTemporaryCollectionCreator;
public Type CollectionItemType { get; set; }
public bool IsMultidimensionalArray { get; set; }
internal bool IsArray { get; set; }
internal bool ShouldCreateWrapper { get; set; }
internal bool CanDeserialize { get; set; }
internal MethodBase ParametrizedConstructor { get; set; }
public JsonArrayContract(Type underlyingType)
: base(underlyingType)
{
ContractType = JsonContractType.Array;
IsArray = base.CreatedType.IsArray;
bool canDeserialize;
Type implementingType;
if (IsArray) {
CollectionItemType = ReflectionUtils.GetCollectionItemType(base.UnderlyingType);
IsReadOnlyOrFixedSize = true;
_genericCollectionDefinitionType = typeof(List<>).MakeGenericType(CollectionItemType);
canDeserialize = true;
IsMultidimensionalArray = (IsArray && base.UnderlyingType.GetArrayRank() > 1);
} else if (typeof(IList).IsAssignableFrom(underlyingType)) {
if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(ICollection<>), out _genericCollectionDefinitionType))
CollectionItemType = _genericCollectionDefinitionType.GetGenericArguments()[0];
else
CollectionItemType = ReflectionUtils.GetCollectionItemType(underlyingType);
if (underlyingType == typeof(IList))
base.CreatedType = typeof(List<object>);
if (CollectionItemType != (Type)null)
ParametrizedConstructor = CollectionUtils.ResolveEnumableCollectionConstructor(underlyingType, CollectionItemType);
IsReadOnlyOrFixedSize = ReflectionUtils.InheritsGenericDefinition(underlyingType, typeof(ReadOnlyCollection<>));
canDeserialize = true;
} else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(ICollection<>), out _genericCollectionDefinitionType)) {
CollectionItemType = _genericCollectionDefinitionType.GetGenericArguments()[0];
if (ReflectionUtils.IsGenericDefinition(underlyingType, typeof(ICollection<>)) || ReflectionUtils.IsGenericDefinition(underlyingType, typeof(IList<>)))
base.CreatedType = typeof(List<>).MakeGenericType(CollectionItemType);
if (ReflectionUtils.IsGenericDefinition(underlyingType, typeof(ISet<>)))
base.CreatedType = typeof(HashSet<>).MakeGenericType(CollectionItemType);
ParametrizedConstructor = CollectionUtils.ResolveEnumableCollectionConstructor(underlyingType, CollectionItemType);
canDeserialize = true;
ShouldCreateWrapper = true;
} else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IEnumerable<>), out implementingType)) {
CollectionItemType = implementingType.GetGenericArguments()[0];
if (ReflectionUtils.IsGenericDefinition(base.UnderlyingType, typeof(IEnumerable<>)))
base.CreatedType = typeof(List<>).MakeGenericType(CollectionItemType);
ParametrizedConstructor = CollectionUtils.ResolveEnumableCollectionConstructor(underlyingType, CollectionItemType);
if (underlyingType.IsGenericType() && underlyingType.GetGenericTypeDefinition() == typeof(IEnumerable<>)) {
_genericCollectionDefinitionType = implementingType;
IsReadOnlyOrFixedSize = false;
ShouldCreateWrapper = false;
canDeserialize = true;
} else {
_genericCollectionDefinitionType = typeof(List<>).MakeGenericType(CollectionItemType);
IsReadOnlyOrFixedSize = true;
ShouldCreateWrapper = true;
canDeserialize = (ParametrizedConstructor != (MethodBase)null);
}
} else {
canDeserialize = false;
ShouldCreateWrapper = true;
}
CanDeserialize = canDeserialize;
if (CollectionItemType != (Type)null)
_isCollectionItemTypeNullableType = ReflectionUtils.IsNullableType(CollectionItemType);
}
internal IWrappedCollection CreateWrapper(object list)
{
if (_genericWrapperCreator == null) {
_genericWrapperType = typeof(CollectionWrapper<>).MakeGenericType(CollectionItemType);
Type type = (!ReflectionUtils.InheritsGenericDefinition(_genericCollectionDefinitionType, typeof(List<>)) && !(_genericCollectionDefinitionType.GetGenericTypeDefinition() == typeof(IEnumerable<>))) ? _genericCollectionDefinitionType : typeof(ICollection<>).MakeGenericType(CollectionItemType);
ConstructorInfo constructor = _genericWrapperType.GetConstructor(new Type[1] {
type
});
_genericWrapperCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall<object>(constructor);
}
return (IWrappedCollection)_genericWrapperCreator(null, list);
}
internal IList CreateTemporaryCollection()
{
if (_genericTemporaryCollectionCreator == null) {
Type type = IsMultidimensionalArray ? typeof(object) : CollectionItemType;
Type type2 = typeof(List<>).MakeGenericType(type);
_genericTemporaryCollectionCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateDefaultConstructor<object>(type2);
}
return (IList)_genericTemporaryCollectionCreator();
}
}
}