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

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; namespace Newtonsoft.Json.Serialization { public class JsonDictionaryContract : JsonContainerContract { private readonly Type _genericCollectionDefinitionType; private Type _genericWrapperType; private ObjectConstructor<object> _genericWrapperCreator; private Func<object> _genericTemporaryDictionaryCreator; private readonly ConstructorInfo _parametrizedConstructor; private ObjectConstructor<object> _parametrizedCreator; 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 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 JsonDictionaryContract(Type underlyingType) : base(underlyingType) { ContractType = JsonContractType.Dictionary; Type keyType; Type valueType; if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IDictionary<, >), out _genericCollectionDefinitionType)) { keyType = TypeExtensions.GetGenericArguments(_genericCollectionDefinitionType)[0]; valueType = TypeExtensions.GetGenericArguments(_genericCollectionDefinitionType)[1]; if (ReflectionUtils.IsGenericDefinition(base.UnderlyingType, typeof(IDictionary<, >))) base.CreatedType = typeof(Dictionary<, >).MakeGenericType(keyType, valueType); IsReadOnlyOrFixedSize = ReflectionUtils.InheritsGenericDefinition(underlyingType, typeof(ReadOnlyDictionary<, >)); } else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IReadOnlyDictionary<, >), out _genericCollectionDefinitionType)) { keyType = TypeExtensions.GetGenericArguments(_genericCollectionDefinitionType)[0]; valueType = TypeExtensions.GetGenericArguments(_genericCollectionDefinitionType)[1]; if (ReflectionUtils.IsGenericDefinition(base.UnderlyingType, typeof(IReadOnlyDictionary<, >))) base.CreatedType = typeof(ReadOnlyDictionary<, >).MakeGenericType(keyType, valueType); IsReadOnlyOrFixedSize = true; } else { ReflectionUtils.GetDictionaryKeyValueTypes(base.UnderlyingType, out keyType, out valueType); if ((object)base.UnderlyingType == typeof(IDictionary)) base.CreatedType = typeof(Dictionary<object, object>); } if ((object)keyType != null && (object)valueType != null) _parametrizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(base.CreatedType, typeof(KeyValuePair<, >).MakeGenericType(keyType, valueType)); ShouldCreateWrapper = !typeof(IDictionary).IsAssignableFrom(base.CreatedType); DictionaryKeyType = keyType; DictionaryValueType = valueType; if (ImmutableCollectionsUtils.TryBuildImmutableForDictionaryContract(underlyingType, DictionaryKeyType, DictionaryValueType, out Type createdType, out ObjectConstructor<object> parameterizedCreator)) { base.CreatedType = createdType; _parametrizedCreator = parameterizedCreator; IsReadOnlyOrFixedSize = true; } } 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.CreateParametrizedConstructor(constructor); } return (IWrappedDictionary)_genericWrapperCreator(dictionary); } internal IDictionary CreateTemporaryDictionary() { if (_genericTemporaryDictionaryCreator == null) { Type type = typeof(Dictionary<, >).MakeGenericType(DictionaryKeyType, DictionaryValueType); _genericTemporaryDictionaryCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateDefaultConstructor<object>(type); } return (IDictionary)_genericTemporaryDictionaryCreator(); } } }