<PackageReference Include="Newtonsoft.Json" Version="6.0.8" />

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 Type _genericCollectionDefinitionType; private Type _genericWrapperType; private ObjectConstructor<object> _genericWrapperCreator; private Func<object> _genericTemporaryCollectionCreator; private readonly ConstructorInfo _parametrizedConstructor; private ObjectConstructor<object> _parametrizedCreator; 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 ObjectConstructor<object> ParametrizedCreator { get { if (_parametrizedCreator == null) _parametrizedCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateParametrizedConstructor(_parametrizedConstructor); return _parametrizedCreator; } } internal bool HasParametrizedCreator { get { if (_parametrizedCreator == null) return (object)_parametrizedConstructor != null; return true; } } 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 = TypeExtensions.GetGenericArguments(_genericCollectionDefinitionType)[0]; else CollectionItemType = ReflectionUtils.GetCollectionItemType(underlyingType); if ((object)underlyingType == typeof(IList)) base.CreatedType = typeof(List<object>); if ((object)CollectionItemType != null) _parametrizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType); IsReadOnlyOrFixedSize = ReflectionUtils.InheritsGenericDefinition(underlyingType, typeof(ReadOnlyCollection<>)); canDeserialize = true; } else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(ICollection<>), out _genericCollectionDefinitionType)) { CollectionItemType = TypeExtensions.GetGenericArguments(_genericCollectionDefinitionType)[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.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType); canDeserialize = true; ShouldCreateWrapper = true; } else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IReadOnlyCollection<>), out implementingType)) { CollectionItemType = TypeExtensions.GetGenericArguments(implementingType)[0]; if (ReflectionUtils.IsGenericDefinition(underlyingType, typeof(IReadOnlyCollection<>)) || ReflectionUtils.IsGenericDefinition(underlyingType, typeof(IReadOnlyList<>))) base.CreatedType = typeof(ReadOnlyCollection<>).MakeGenericType(CollectionItemType); _genericCollectionDefinitionType = typeof(List<>).MakeGenericType(CollectionItemType); _parametrizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(base.CreatedType, CollectionItemType); IsReadOnlyOrFixedSize = true; canDeserialize = HasParametrizedCreator; } else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IEnumerable<>), out implementingType)) { CollectionItemType = TypeExtensions.GetGenericArguments(implementingType)[0]; if (ReflectionUtils.IsGenericDefinition(base.UnderlyingType, typeof(IEnumerable<>))) base.CreatedType = typeof(List<>).MakeGenericType(CollectionItemType); _parametrizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType); if (underlyingType.IsGenericType() && (object)underlyingType.GetGenericTypeDefinition() == typeof(IEnumerable<>)) { _genericCollectionDefinitionType = implementingType; IsReadOnlyOrFixedSize = false; ShouldCreateWrapper = false; canDeserialize = true; } else { _genericCollectionDefinitionType = typeof(List<>).MakeGenericType(CollectionItemType); IsReadOnlyOrFixedSize = true; ShouldCreateWrapper = true; canDeserialize = HasParametrizedCreator; } } else { canDeserialize = false; ShouldCreateWrapper = true; } CanDeserialize = canDeserialize; if (ImmutableCollectionsUtils.TryBuildImmutableForArrayContract(underlyingType, CollectionItemType, out Type createdType, out ObjectConstructor<object> parameterizedCreator)) { base.CreatedType = createdType; _parametrizedCreator = parameterizedCreator; IsReadOnlyOrFixedSize = true; CanDeserialize = true; } } internal IWrappedCollection CreateWrapper(object list) { if (_genericWrapperCreator == null) { _genericWrapperType = typeof(CollectionWrapper<>).MakeGenericType(CollectionItemType); Type type = (!ReflectionUtils.InheritsGenericDefinition(_genericCollectionDefinitionType, typeof(List<>)) && (object)_genericCollectionDefinitionType.GetGenericTypeDefinition() != typeof(IEnumerable<>)) ? _genericCollectionDefinitionType : typeof(ICollection<>).MakeGenericType(CollectionItemType); ConstructorInfo constructor = TypeExtensions.GetConstructor(_genericWrapperType, new Type[1] { type }); _genericWrapperCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateParametrizedConstructor(constructor); } return (IWrappedCollection)_genericWrapperCreator(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(); } } }