JsonDictionaryContract
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 JsonDictionaryContract : 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> _genericTemporaryDictionaryCreator;
private readonly ConstructorInfo _parameterizedConstructor;
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})]
private ObjectConstructor<object> _overrideCreator;
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})]
private ObjectConstructor<object> _parameterizedCreator;
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1,
1
})]
[field: System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1,
1
})]
public Func<string, string> DictionaryKeyResolver {
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1,
1
})]
get;
[param: System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1,
1
})]
set;
}
public Type DictionaryKeyType { get; }
public Type DictionaryValueType { get; }
internal JsonContract KeyContract { get; set; }
internal bool ShouldCreateWrapper { get; }
[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;
}
}
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 JsonDictionaryContract(Type underlyingType)
: base(underlyingType)
{
ContractType = JsonContractType.Dictionary;
Type keyType;
Type valueType;
if (ReflectionUtils.ImplementsGenericDefinition(NonNullableUnderlyingType, typeof(IDictionary<, >), out _genericCollectionDefinitionType)) {
keyType = TypeExtensions.GetGenericArguments(_genericCollectionDefinitionType)[0];
valueType = TypeExtensions.GetGenericArguments(_genericCollectionDefinitionType)[1];
if (ReflectionUtils.IsGenericDefinition(NonNullableUnderlyingType, typeof(IDictionary<, >)))
base.CreatedType = typeof(Dictionary<, >).MakeGenericType(keyType, valueType);
else if (NonNullableUnderlyingType.IsGenericType() && NonNullableUnderlyingType.GetGenericTypeDefinition().FullName == "System.Collections.Concurrent.ConcurrentDictionary`2") {
ShouldCreateWrapper = true;
}
IsReadOnlyOrFixedSize = ReflectionUtils.InheritsGenericDefinition(NonNullableUnderlyingType, typeof(ReadOnlyDictionary<, >));
} else if (ReflectionUtils.ImplementsGenericDefinition(NonNullableUnderlyingType, typeof(IReadOnlyDictionary<, >), out _genericCollectionDefinitionType)) {
keyType = TypeExtensions.GetGenericArguments(_genericCollectionDefinitionType)[0];
valueType = TypeExtensions.GetGenericArguments(_genericCollectionDefinitionType)[1];
if (ReflectionUtils.IsGenericDefinition(NonNullableUnderlyingType, typeof(IReadOnlyDictionary<, >)))
base.CreatedType = typeof(ReadOnlyDictionary<, >).MakeGenericType(keyType, valueType);
IsReadOnlyOrFixedSize = true;
} else {
ReflectionUtils.GetDictionaryKeyValueTypes(NonNullableUnderlyingType, out keyType, out valueType);
if ((object)NonNullableUnderlyingType == typeof(IDictionary))
base.CreatedType = typeof(Dictionary<object, object>);
}
if ((object)keyType != null && (object)valueType != null) {
_parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(base.CreatedType, typeof(KeyValuePair<, >).MakeGenericType(keyType, valueType), typeof(IDictionary<, >).MakeGenericType(keyType, valueType));
if (!HasParameterizedCreatorInternal && NonNullableUnderlyingType.get_Name() == "FSharpMap`2") {
FSharpUtils.EnsureInitialized(NonNullableUnderlyingType.Assembly());
_parameterizedCreator = FSharpUtils.Instance.CreateMap(keyType, valueType);
}
}
if (!typeof(IDictionary).IsAssignableFrom(base.CreatedType))
ShouldCreateWrapper = true;
DictionaryKeyType = keyType;
DictionaryValueType = valueType;
if ((object)DictionaryKeyType != null && (object)DictionaryValueType != null && ImmutableCollectionsUtils.TryBuildImmutableForDictionaryContract(NonNullableUnderlyingType, DictionaryKeyType, DictionaryValueType, out Type createdType, out ObjectConstructor<object> parameterizedCreator)) {
base.CreatedType = createdType;
_parameterizedCreator = parameterizedCreator;
IsReadOnlyOrFixedSize = true;
}
}
[System.Runtime.CompilerServices.NullableContext(1)]
internal IWrappedDictionary CreateWrapper(object dictionary)
{
if (_genericWrapperCreator == null) {
_genericWrapperType = typeof(DictionaryWrapper<, >).MakeGenericType(DictionaryKeyType, DictionaryValueType);
ConstructorInfo constructor = TypeExtensions.GetConstructor(_genericWrapperType, new Type[1] {
_genericCollectionDefinitionType
});
_genericWrapperCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateParameterizedConstructor(constructor);
}
return (IWrappedDictionary)_genericWrapperCreator(dictionary);
}
[System.Runtime.CompilerServices.NullableContext(1)]
internal IDictionary CreateTemporaryDictionary()
{
if (_genericTemporaryDictionaryCreator == null) {
Type type = typeof(Dictionary<, >).MakeGenericType(DictionaryKeyType ?? typeof(object), DictionaryValueType ?? typeof(object));
_genericTemporaryDictionaryCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateDefaultConstructor<object>(type);
}
return (IDictionary)_genericTemporaryDictionaryCreator();
}
}
}