2
// Copyright (c) 2007 James Newton-King
4
// Permission is hereby granted, free of charge, to any person
5
// obtaining a copy of this software and associated documentation
6
// files (the "Software"), to deal in the Software without
7
// restriction, including without limitation the rights to use,
8
// copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the
10
// Software is furnished to do so, subject to the following
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
// OTHER DEALINGS IN THE SOFTWARE.
27
using System.Collections.Generic;
28
using System.Reflection;
29
using Newtonsoft.Json.Utilities;
30
using System.Collections;
32
using Newtonsoft.Json.Utilities.LinqBridge;
35
namespace Newtonsoft.Json.Serialization
38
/// Contract details for a <see cref="Type"/> used by the <see cref="JsonSerializer"/>.
40
public class JsonDictionaryContract : JsonContainerContract
43
/// Gets or sets the property name resolver.
45
/// <value>The property name resolver.</value>
46
public Func<string, string> PropertyNameResolver { get; set; }
49
/// Gets the <see cref="Type"/> of the dictionary keys.
51
/// <value>The <see cref="Type"/> of the dictionary keys.</value>
52
public Type DictionaryKeyType { get; private set; }
54
/// Gets the <see cref="Type"/> of the dictionary values.
56
/// <value>The <see cref="Type"/> of the dictionary values.</value>
57
public Type DictionaryValueType { get; private set; }
59
internal JsonContract KeyContract { get; set; }
61
private readonly bool _isDictionaryValueTypeNullableType;
62
private readonly Type _genericCollectionDefinitionType;
63
private Type _genericWrapperType;
64
private MethodCall<object, object> _genericWrapperCreator;
67
/// Initializes a new instance of the <see cref="JsonDictionaryContract"/> class.
69
/// <param name="underlyingType">The underlying type for the contract.</param>
70
public JsonDictionaryContract(Type underlyingType)
71
: base(underlyingType)
73
ContractType = JsonContractType.Dictionary;
77
if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IDictionary<,>), out _genericCollectionDefinitionType))
79
keyType = _genericCollectionDefinitionType.GetGenericArguments()[0];
80
valueType = _genericCollectionDefinitionType.GetGenericArguments()[1];
84
ReflectionUtils.GetDictionaryKeyValueTypes(UnderlyingType, out keyType, out valueType);
87
DictionaryKeyType = keyType;
88
DictionaryValueType = valueType;
90
if (DictionaryValueType != null)
91
_isDictionaryValueTypeNullableType = ReflectionUtils.IsNullableType(DictionaryValueType);
93
if (IsTypeGenericDictionaryInterface(UnderlyingType))
95
CreatedType = ReflectionUtils.MakeGenericType(typeof(Dictionary<,>), keyType, valueType);
97
else if (UnderlyingType == typeof(IDictionary))
99
CreatedType = typeof (Dictionary<object, object>);
103
internal IWrappedDictionary CreateWrapper(object dictionary)
105
if (dictionary is IDictionary && (DictionaryValueType == null || !_isDictionaryValueTypeNullableType))
106
return new DictionaryWrapper<object, object>((IDictionary)dictionary);
108
if (_genericWrapperCreator == null)
110
_genericWrapperType = ReflectionUtils.MakeGenericType(typeof(DictionaryWrapper<,>), DictionaryKeyType, DictionaryValueType);
112
ConstructorInfo genericWrapperConstructor = _genericWrapperType.GetConstructor(new[] { _genericCollectionDefinitionType });
113
_genericWrapperCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall<object>(genericWrapperConstructor);
116
return (IWrappedDictionary)_genericWrapperCreator(null, dictionary);
119
private bool IsTypeGenericDictionaryInterface(Type type)
121
if (!type.IsGenericType())
124
Type genericDefinition = type.GetGenericTypeDefinition();
126
return (genericDefinition == typeof(IDictionary<,>));
b'\\ No newline at end of file'