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;
37
namespace Newtonsoft.Json.Serialization
40
/// Contract details for a <see cref="Type"/> used by the <see cref="JsonSerializer"/>.
42
public class JsonArrayContract : JsonContainerContract
45
/// Gets the <see cref="Type"/> of the collection items.
47
/// <value>The <see cref="Type"/> of the collection items.</value>
48
public Type CollectionItemType { get; private set; }
51
/// Gets a value indicating whether the collection type is a multidimensional array.
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; }
56
private readonly bool _isCollectionItemTypeNullableType;
57
private readonly Type _genericCollectionDefinitionType;
58
private Type _genericWrapperType;
59
private MethodCall<object, object> _genericWrapperCreator;
62
/// Initializes a new instance of the <see cref="JsonArrayContract"/> class.
64
/// <param name="underlyingType">The underlying type for the contract.</param>
65
public JsonArrayContract(Type underlyingType)
66
: base(underlyingType)
68
ContractType = JsonContractType.Array;
70
if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(ICollection<>), out _genericCollectionDefinitionType))
72
CollectionItemType = _genericCollectionDefinitionType.GetGenericArguments()[0];
74
else if (underlyingType.IsGenericType() && underlyingType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
76
_genericCollectionDefinitionType = typeof (IEnumerable<>);
77
CollectionItemType = underlyingType.GetGenericArguments()[0];
81
CollectionItemType = ReflectionUtils.GetCollectionItemType(UnderlyingType);
84
if (CollectionItemType != null)
85
_isCollectionItemTypeNullableType = ReflectionUtils.IsNullableType(CollectionItemType);
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);
94
IsMultidimensionalArray = (UnderlyingType.IsArray && UnderlyingType.GetArrayRank() > 1);
97
internal IWrappedCollection CreateWrapper(object list)
99
if ((list is IList && (CollectionItemType == null || !_isCollectionItemTypeNullableType))
100
|| UnderlyingType.IsArray)
101
return new CollectionWrapper<object>((IList)list);
103
if (_genericCollectionDefinitionType != null)
105
EnsureGenericWrapperCreator();
106
return (IWrappedCollection) _genericWrapperCreator(null, list);
110
IList values = ((IEnumerable) list).Cast<object>().ToList();
112
if (CollectionItemType != null)
114
Array array = Array.CreateInstance(CollectionItemType, values.Count);
115
for (int i = 0; i < values.Count; i++)
117
array.SetValue(values[i], i);
123
return new CollectionWrapper<object>(values);
127
private void EnsureGenericWrapperCreator()
129
if (_genericWrapperCreator == null)
131
_genericWrapperType = ReflectionUtils.MakeGenericType(typeof (CollectionWrapper<>), CollectionItemType);
133
Type constructorArgument;
135
if (ReflectionUtils.InheritsGenericDefinition(_genericCollectionDefinitionType, typeof(List<>))
136
|| _genericCollectionDefinitionType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
137
constructorArgument = ReflectionUtils.MakeGenericType(typeof(ICollection<>), CollectionItemType);
139
constructorArgument = _genericCollectionDefinitionType;
141
ConstructorInfo genericWrapperConstructor = _genericWrapperType.GetConstructor(new[] { constructorArgument });
142
_genericWrapperCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall<object>(genericWrapperConstructor);
146
private bool IsTypeGenericCollectionInterface(Type type)
148
if (!type.IsGenericType())
151
Type genericDefinition = type.GetGenericTypeDefinition();
153
return (genericDefinition == typeof(IList<>)
154
|| genericDefinition == typeof(ICollection<>)
155
|| genericDefinition == typeof(IEnumerable<>));
158
#if !(PORTABLE || NET20 || NET35 || WINDOWS_PHONE)
159
private bool IsTypeGenericSetnterface(Type type)
161
if (!type.IsGenericType())
164
Type genericDefinition = type.GetGenericTypeDefinition();
166
return (genericDefinition == typeof(ISet<>));
b'\\ No newline at end of file'