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;
using System.Runtime.CompilerServices;
namespace Newtonsoft.Json.Serialization
{
[System.Runtime.CompilerServices.NullableContext(2)]
[System.Runtime.CompilerServices.Nullable(0)]
public class JsonArrayContract : JsonContainerContract
{
private readonly Type _genericCollectionDefinitionType;
private Type _genericWrapperType;
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})]
private ObjectConstructor<object> _genericWrapperCreator;
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})]
private Func<object> _genericTemporaryCollectionCreator;
private readonly ConstructorInfo _parameterizedConstructor;
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})]
private ObjectConstructor<object> _parameterizedCreator;
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})]
private ObjectConstructor<object> _overrideCreator;
public Type CollectionItemType { get; }
public bool IsMultidimensionalArray { get; }
internal bool IsArray { get; }
internal bool ShouldCreateWrapper { get; }
internal bool CanDeserialize { get; set; }
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})]
internal ObjectConstructor<object> ParameterizedCreator {
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})]
get {
if (_parameterizedCreator == null && (object)_parameterizedConstructor != null)
_parameterizedCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateParameterizedConstructor(_parameterizedConstructor);
return _parameterizedCreator;
}
}
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})]
public ObjectConstructor<object> OverrideCreator {
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})]
get {
return _overrideCreator;
}
[param: System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})]
set {
_overrideCreator = value;
CanDeserialize = true;
}
}
public bool HasParameterizedCreator { get; set; }
internal bool HasParameterizedCreatorInternal {
get {
if (!HasParameterizedCreator && _parameterizedCreator == null)
return (object)_parameterizedConstructor != null;
return true;
}
}
[System.Runtime.CompilerServices.NullableContext(1)]
public JsonArrayContract(Type underlyingType)
: base(underlyingType)
{
ContractType = JsonContractType.Array;
IsArray = (base.CreatedType.IsArray || (NonNullableUnderlyingType.IsGenericType() && NonNullableUnderlyingType.GetGenericTypeDefinition().FullName == "System.Linq.EmptyPartition`1"));
bool canDeserialize;
Type implementingType;
if (IsArray) {
CollectionItemType = ReflectionUtils.GetCollectionItemType(base.UnderlyingType);
IsReadOnlyOrFixedSize = true;
_genericCollectionDefinitionType = typeof(List<>).MakeGenericType(CollectionItemType);
canDeserialize = true;
IsMultidimensionalArray = (base.CreatedType.IsArray && base.UnderlyingType.GetArrayRank() > 1);
} else if (typeof(IList).IsAssignableFrom(NonNullableUnderlyingType)) {
if (ReflectionUtils.ImplementsGenericDefinition(NonNullableUnderlyingType, typeof(ICollection<>), out _genericCollectionDefinitionType))
CollectionItemType = _genericCollectionDefinitionType.GetGenericArguments()[0];
else
CollectionItemType = ReflectionUtils.GetCollectionItemType(NonNullableUnderlyingType);
if ((object)NonNullableUnderlyingType == typeof(IList))
base.CreatedType = typeof(List<object>);
if ((object)CollectionItemType != null)
_parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(NonNullableUnderlyingType, CollectionItemType);
IsReadOnlyOrFixedSize = ReflectionUtils.InheritsGenericDefinition(NonNullableUnderlyingType, typeof(ReadOnlyCollection<>));
canDeserialize = true;
} else if (ReflectionUtils.ImplementsGenericDefinition(NonNullableUnderlyingType, typeof(ICollection<>), out _genericCollectionDefinitionType)) {
CollectionItemType = _genericCollectionDefinitionType.GetGenericArguments()[0];
if (ReflectionUtils.IsGenericDefinition(NonNullableUnderlyingType, typeof(ICollection<>)) || ReflectionUtils.IsGenericDefinition(NonNullableUnderlyingType, typeof(IList<>)))
base.CreatedType = typeof(List<>).MakeGenericType(CollectionItemType);
_parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(NonNullableUnderlyingType, CollectionItemType);
canDeserialize = true;
ShouldCreateWrapper = true;
} else if (ReflectionUtils.ImplementsGenericDefinition(NonNullableUnderlyingType, typeof(IEnumerable<>), out implementingType)) {
CollectionItemType = implementingType.GetGenericArguments()[0];
if (ReflectionUtils.IsGenericDefinition(base.UnderlyingType, typeof(IEnumerable<>)))
base.CreatedType = typeof(List<>).MakeGenericType(CollectionItemType);
_parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(NonNullableUnderlyingType, CollectionItemType);
if (NonNullableUnderlyingType.IsGenericType() && (object)NonNullableUnderlyingType.GetGenericTypeDefinition() == typeof(IEnumerable<>)) {
_genericCollectionDefinitionType = implementingType;
IsReadOnlyOrFixedSize = false;
ShouldCreateWrapper = false;
canDeserialize = true;
} else {
_genericCollectionDefinitionType = typeof(List<>).MakeGenericType(CollectionItemType);
IsReadOnlyOrFixedSize = true;
ShouldCreateWrapper = true;
canDeserialize = HasParameterizedCreatorInternal;
}
} else {
canDeserialize = false;
ShouldCreateWrapper = true;
}
CanDeserialize = canDeserialize;
if ((object)CollectionItemType != null && ReflectionUtils.IsNullableType(CollectionItemType) && (ReflectionUtils.InheritsGenericDefinition(base.CreatedType, typeof(List<>), out implementingType) || (IsArray && !IsMultidimensionalArray)))
ShouldCreateWrapper = true;
if ((object)CollectionItemType != null && ImmutableCollectionsUtils.TryBuildImmutableForArrayContract(NonNullableUnderlyingType, CollectionItemType, out Type createdType, out ObjectConstructor<object> parameterizedCreator)) {
base.CreatedType = createdType;
_parameterizedCreator = parameterizedCreator;
IsReadOnlyOrFixedSize = true;
CanDeserialize = true;
}
}
[System.Runtime.CompilerServices.NullableContext(1)]
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 = _genericWrapperType.GetConstructor(new Type[1] {
type
});
_genericWrapperCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateParameterizedConstructor(constructor);
}
return (IWrappedCollection)_genericWrapperCreator(list);
}
[System.Runtime.CompilerServices.NullableContext(1)]
internal IList CreateTemporaryCollection()
{
if (_genericTemporaryCollectionCreator == null) {
Type type = (IsMultidimensionalArray || (object)CollectionItemType == null) ? typeof(object) : CollectionItemType;
Type type2 = typeof(List<>).MakeGenericType(type);
_genericTemporaryCollectionCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateDefaultConstructor<object>(type2);
}
return (IList)_genericTemporaryCollectionCreator();
}
}
}