~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/Newtonsoft.Json/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#if NET20
 
32
using Newtonsoft.Json.Utilities.LinqBridge;
 
33
#else
 
34
using System.Linq;
 
35
#endif
 
36
 
 
37
namespace Newtonsoft.Json.Serialization
 
38
{
 
39
  /// <summary>
 
40
  /// Contract details for a <see cref="Type"/> used by the <see cref="JsonSerializer"/>.
 
41
  /// </summary>
 
42
  public class JsonArrayContract : JsonContainerContract
 
43
  {
 
44
    /// <summary>
 
45
    /// Gets the <see cref="Type"/> of the collection items.
 
46
    /// </summary>
 
47
    /// <value>The <see cref="Type"/> of the collection items.</value>
 
48
    public Type CollectionItemType { get; private set; }
 
49
 
 
50
    /// <summary>
 
51
    /// Gets a value indicating whether the collection type is a multidimensional array.
 
52
    /// </summary>
 
53
    /// <value><c>true</c> if the collection type is a multidimensional array; otherwise, <c>false</c>.</value>
 
54
    public bool IsMultidimensionalArray { get; private set; }
 
55
 
 
56
    private readonly bool _isCollectionItemTypeNullableType;
 
57
    private readonly Type _genericCollectionDefinitionType;
 
58
    private Type _genericWrapperType;
 
59
    private MethodCall<object, object> _genericWrapperCreator;
 
60
 
 
61
    /// <summary>
 
62
    /// Initializes a new instance of the <see cref="JsonArrayContract"/> class.
 
63
    /// </summary>
 
64
    /// <param name="underlyingType">The underlying type for the contract.</param>
 
65
    public JsonArrayContract(Type underlyingType)
 
66
      : base(underlyingType)
 
67
    {
 
68
      ContractType = JsonContractType.Array;
 
69
      
 
70
      if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(ICollection<>), out _genericCollectionDefinitionType))
 
71
      {
 
72
        CollectionItemType = _genericCollectionDefinitionType.GetGenericArguments()[0];
 
73
      }
 
74
      else if (underlyingType.IsGenericType() && underlyingType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
 
75
      {
 
76
        _genericCollectionDefinitionType =  typeof (IEnumerable<>);
 
77
        CollectionItemType = underlyingType.GetGenericArguments()[0];
 
78
      }
 
79
      else
 
80
      {
 
81
        CollectionItemType = ReflectionUtils.GetCollectionItemType(UnderlyingType);
 
82
      }
 
83
 
 
84
      if (CollectionItemType != null)
 
85
        _isCollectionItemTypeNullableType = ReflectionUtils.IsNullableType(CollectionItemType);
 
86
 
 
87
      if (IsTypeGenericCollectionInterface(UnderlyingType))
 
88
        CreatedType = ReflectionUtils.MakeGenericType(typeof(List<>), CollectionItemType);
 
89
#if !(PORTABLE || NET20 || NET35 || WINDOWS_PHONE)
 
90
      else if (IsTypeGenericSetnterface(UnderlyingType))
 
91
        CreatedType = ReflectionUtils.MakeGenericType(typeof(HashSet<>), CollectionItemType);
 
92
#endif
 
93
 
 
94
      IsMultidimensionalArray = (UnderlyingType.IsArray && UnderlyingType.GetArrayRank() > 1);
 
95
    }
 
96
 
 
97
    internal IWrappedCollection CreateWrapper(object list)
 
98
    {
 
99
      if ((list is IList && (CollectionItemType == null || !_isCollectionItemTypeNullableType))
 
100
        || UnderlyingType.IsArray)
 
101
        return new CollectionWrapper<object>((IList)list);
 
102
 
 
103
      if (_genericCollectionDefinitionType != null)
 
104
      {
 
105
        EnsureGenericWrapperCreator();
 
106
        return (IWrappedCollection) _genericWrapperCreator(null, list);
 
107
      }
 
108
      else
 
109
      {
 
110
        IList values = ((IEnumerable) list).Cast<object>().ToList();
 
111
 
 
112
        if (CollectionItemType != null)
 
113
        {
 
114
          Array array = Array.CreateInstance(CollectionItemType, values.Count);
 
115
          for (int i = 0; i < values.Count; i++)
 
116
          {
 
117
            array.SetValue(values[i], i);
 
118
          }
 
119
 
 
120
          values = array;
 
121
        }
 
122
 
 
123
        return new CollectionWrapper<object>(values);
 
124
      }
 
125
    }
 
126
 
 
127
    private void EnsureGenericWrapperCreator()
 
128
    {
 
129
      if (_genericWrapperCreator == null)
 
130
      {
 
131
        _genericWrapperType = ReflectionUtils.MakeGenericType(typeof (CollectionWrapper<>), CollectionItemType);
 
132
 
 
133
        Type constructorArgument;
 
134
 
 
135
        if (ReflectionUtils.InheritsGenericDefinition(_genericCollectionDefinitionType, typeof(List<>))
 
136
          || _genericCollectionDefinitionType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
 
137
          constructorArgument = ReflectionUtils.MakeGenericType(typeof(ICollection<>), CollectionItemType);
 
138
        else
 
139
          constructorArgument = _genericCollectionDefinitionType;
 
140
 
 
141
        ConstructorInfo genericWrapperConstructor = _genericWrapperType.GetConstructor(new[] { constructorArgument });
 
142
        _genericWrapperCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall<object>(genericWrapperConstructor);
 
143
      }
 
144
    }
 
145
 
 
146
    private bool IsTypeGenericCollectionInterface(Type type)
 
147
    {
 
148
      if (!type.IsGenericType())
 
149
        return false;
 
150
 
 
151
      Type genericDefinition = type.GetGenericTypeDefinition();
 
152
 
 
153
      return (genericDefinition == typeof(IList<>)
 
154
              || genericDefinition == typeof(ICollection<>)
 
155
              || genericDefinition == typeof(IEnumerable<>));
 
156
    }
 
157
 
 
158
#if !(PORTABLE || NET20 || NET35 || WINDOWS_PHONE)
 
159
    private bool IsTypeGenericSetnterface(Type type)
 
160
    {
 
161
      if (!type.IsGenericType())
 
162
        return false;
 
163
 
 
164
      Type genericDefinition = type.GetGenericTypeDefinition();
 
165
 
 
166
      return (genericDefinition == typeof(ISet<>));
 
167
    }
 
168
#endif
 
169
  }
 
170
}
 
 
b'\\ No newline at end of file'