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

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.Reflection; namespace Newtonsoft.Json.Serialization { public class JsonDictionaryContract : JsonContainerContract { private readonly bool _isDictionaryValueTypeNullableType; private readonly Type _genericCollectionDefinitionType; private Type _genericWrapperType; private MethodCall<object, object> _genericWrapperCreator; private Func<object> _genericTemporaryDictionaryCreator; public Func<string, string> PropertyNameResolver { get; set; } public Type DictionaryKeyType { get; set; } public Type DictionaryValueType { get; set; } internal JsonContract KeyContract { get; set; } internal bool ShouldCreateWrapper { get; set; } internal MethodBase ParametrizedConstructor { get; set; } public JsonDictionaryContract(Type underlyingType) : base(underlyingType) { ContractType = JsonContractType.Dictionary; Type keyType; Type valueType; if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IDictionary<, >), out _genericCollectionDefinitionType)) { keyType = _genericCollectionDefinitionType.GetGenericArguments()[0]; valueType = _genericCollectionDefinitionType.GetGenericArguments()[1]; if (ReflectionUtils.IsGenericDefinition(base.UnderlyingType, typeof(IDictionary<, >))) base.CreatedType = typeof(Dictionary<, >).MakeGenericType(keyType, valueType); } else { ReflectionUtils.GetDictionaryKeyValueTypes(base.UnderlyingType, out keyType, out valueType); if (base.UnderlyingType == typeof(IDictionary)) base.CreatedType = typeof(Dictionary<object, object>); } if (keyType != (Type)null && valueType != (Type)null) ParametrizedConstructor = CollectionUtils.ResolveEnumableCollectionConstructor(base.CreatedType, typeof(KeyValuePair<, >).MakeGenericType(keyType, valueType)); ShouldCreateWrapper = !typeof(IDictionary).IsAssignableFrom(base.CreatedType); DictionaryKeyType = keyType; DictionaryValueType = valueType; if (DictionaryValueType != (Type)null) _isDictionaryValueTypeNullableType = ReflectionUtils.IsNullableType(DictionaryValueType); } internal IWrappedDictionary CreateWrapper(object dictionary) { if (_genericWrapperCreator == null) { _genericWrapperType = typeof(DictionaryWrapper<, >).MakeGenericType(DictionaryKeyType, DictionaryValueType); ConstructorInfo constructor = _genericWrapperType.GetConstructor(new Type[1] { _genericCollectionDefinitionType }); _genericWrapperCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall<object>(constructor); } return (IWrappedDictionary)_genericWrapperCreator(null, dictionary); } internal IDictionary CreateTemporaryDictionary() { if (_genericTemporaryDictionaryCreator == null) { Type type = typeof(Dictionary<, >).MakeGenericType(DictionaryKeyType, DictionaryValueType); _genericTemporaryDictionaryCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateDefaultConstructor<object>(type); } return (IDictionary)_genericTemporaryDictionaryCreator(); } } }