Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json\Serialization\JsonDictionaryContract.cs

Symbol Coverage: 100.00% (23 of 23)

Branch Coverage: 100.00% (15 of 15)

Cyclomatic Complexity Avg: 1.71 Max:3

Code Lines: 24


L V Source
1
#region License
2
// Copyright (c) 2007 James Newton-King
3
//
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
11
// conditions:
12
//
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
15
//
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.
24
#endregion
25

  
26
using System;
27
using System.Collections.Generic;
28
using System.Reflection;
29
using Newtonsoft.Json.Utilities;
30
using System.Collections;
31

  
32
namespace Newtonsoft.Json.Serialization
33
{
34
  /// <summary>
35
  /// Contract details for a <see cref="Type"/> used by the <see cref="JsonSerializer"/>.
36
  /// </summary>
37
  public class JsonDictionaryContract : JsonContract
38
  {
39
    internal Type DictionaryKeyType { get; private set; }
40
    internal Type DictionaryValueType { get; private set; }
41

  
42
    private readonly Type _genericCollectionDefinitionType;
43
    private Type _genericWrapperType;
44
    private MethodCall<object, object> _genericWrapperCreator;
45

  
46
    /// <summary>
47
    /// Initializes a new instance of the <see cref="JsonDictionaryContract"/> class.
48
    /// </summary>
49
    /// <param name="underlyingType">The underlying type for the contract.</param>
50
 16
    public JsonDictionaryContract(Type underlyingType)
51
 16
      : base(underlyingType)
52
    {
53
      Type keyType;
54
      Type valueType;
55
 16
      if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IDictionary<,>), out _genericCollectionDefinitionType))
56
      {
57
 15
        keyType = _genericCollectionDefinitionType.GetGenericArguments()[0];
58
 15
        valueType = _genericCollectionDefinitionType.GetGenericArguments()[1];
59
      }
60
      else
61
      {
62
 1
        ReflectionUtils.GetDictionaryKeyValueTypes(UnderlyingType, out keyType, out valueType);
63
      }
64

  
65
 16
      DictionaryKeyType = keyType;
66
 16
      DictionaryValueType = valueType;
67

  
68
 16
      if (IsTypeGenericDictionaryInterface(UnderlyingType))
69
      {
70
 1
        CreatedType = ReflectionUtils.MakeGenericType(typeof(Dictionary<,>), keyType, valueType);
71
      }
72
 16
    }
73

  
74
    internal IWrappedDictionary CreateWrapper(object dictionary)
75
    {
76
 20446
      if (dictionary is IDictionary)
77
 20444
        return new DictionaryWrapper<object, object>((IDictionary)dictionary);
78

  
79
 2
      if (_genericWrapperType == null)
80
      {
81
 1
        _genericWrapperType = ReflectionUtils.MakeGenericType(typeof(DictionaryWrapper<,>), DictionaryKeyType, DictionaryValueType);
82

  
83
 1
        ConstructorInfo genericWrapperConstructor = _genericWrapperType.GetConstructor(new[] { _genericCollectionDefinitionType });
84
 1
        _genericWrapperCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall<object>(genericWrapperConstructor);
85
      }
86

  
87
 2
      return (IWrappedDictionary)_genericWrapperCreator(null, dictionary);
88
 20446
    }
89

  
90
    private bool IsTypeGenericDictionaryInterface(Type type)
91
    {
92
 16
      if (!type.IsGenericType)
93
 4
        return false;
94

  
95
 12
      Type genericDefinition = type.GetGenericTypeDefinition();
96

  
97
 12
      return (genericDefinition == typeof(IDictionary<,>));
98
 16
    }
99
  }
100
}