| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections; |
| | | 5 | | using System.Collections.Concurrent; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using System.Diagnostics; |
| | | 8 | | using System.Diagnostics.CodeAnalysis; |
| | | 9 | | using System.Reflection; |
| | | 10 | | using System.Text.Json.Reflection; |
| | | 11 | | using System.Text.Json.Serialization.Metadata; |
| | | 12 | | |
| | | 13 | | namespace System.Text.Json.Serialization.Converters |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Converter factory for all IEnumerable types. |
| | | 17 | | /// </summary> |
| | | 18 | | [RequiresDynamicCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)] |
| | | 19 | | internal sealed class IEnumerableConverterFactory : JsonConverterFactory |
| | | 20 | | { |
| | 0 | 21 | | private static readonly IDictionaryConverter<IDictionary> s_converterForIDictionary = new IDictionaryConverter<I |
| | 0 | 22 | | private static readonly IEnumerableConverter<IEnumerable> s_converterForIEnumerable = new IEnumerableConverter<I |
| | 0 | 23 | | private static readonly IListConverter<IList> s_converterForIList = new IListConverter<IList>(); |
| | | 24 | | |
| | | 25 | | [RequiresUnreferencedCode(JsonSerializer.SerializationUnreferencedCodeMessage)] |
| | 3 | 26 | | public IEnumerableConverterFactory() { } |
| | | 27 | | |
| | | 28 | | public override bool CanConvert(Type typeToConvert) |
| | 3303 | 29 | | { |
| | 3303 | 30 | | return typeof(IEnumerable).IsAssignableFrom(typeToConvert); |
| | 3303 | 31 | | } |
| | | 32 | | |
| | | 33 | | [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", |
| | | 34 | | Justification = "The ctor is marked RequiresUnreferencedCode.")] |
| | | 35 | | public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) |
| | 1002 | 36 | | { |
| | | 37 | | Type converterType; |
| | | 38 | | Type[] genericArgs; |
| | 1002 | 39 | | Type? elementType = null; |
| | 1002 | 40 | | Type? dictionaryKeyType = null; |
| | | 41 | | Type? actualTypeToConvert; |
| | | 42 | | |
| | | 43 | | // Array |
| | 1002 | 44 | | if (typeToConvert.IsArray) |
| | 0 | 45 | | { |
| | | 46 | | // Verify that we don't have a multidimensional array. |
| | 0 | 47 | | if (typeToConvert.GetArrayRank() > 1) |
| | 0 | 48 | | { |
| | 0 | 49 | | return UnsupportedTypeConverterFactory.CreateUnsupportedConverterForType(typeToConvert); |
| | | 50 | | } |
| | | 51 | | |
| | 0 | 52 | | converterType = typeof(ArrayConverter<,>); |
| | 0 | 53 | | elementType = typeToConvert.GetElementType(); |
| | 0 | 54 | | } |
| | | 55 | | // List<> or deriving from List<> |
| | 1002 | 56 | | else if ((actualTypeToConvert = typeToConvert.GetCompatibleGenericBaseClass(typeof(List<>))) != null) |
| | 638 | 57 | | { |
| | 638 | 58 | | converterType = typeof(ListOfTConverter<,>); |
| | 638 | 59 | | elementType = actualTypeToConvert.GetGenericArguments()[0]; |
| | 638 | 60 | | } |
| | | 61 | | // Dictionary<TKey, TValue> or deriving from Dictionary<TKey, TValue> |
| | 364 | 62 | | else if ((actualTypeToConvert = typeToConvert.GetCompatibleGenericBaseClass(typeof(Dictionary<,>))) != null) |
| | 364 | 63 | | { |
| | 364 | 64 | | genericArgs = actualTypeToConvert.GetGenericArguments(); |
| | 364 | 65 | | converterType = typeof(DictionaryOfTKeyTValueConverter<,,>); |
| | 364 | 66 | | dictionaryKeyType = genericArgs[0]; |
| | 364 | 67 | | elementType = genericArgs[1]; |
| | 364 | 68 | | } |
| | | 69 | | // Immutable dictionaries from System.Collections.Immutable, e.g. ImmutableDictionary<TKey, TValue> |
| | 0 | 70 | | else if (typeToConvert.IsImmutableDictionaryType()) |
| | 0 | 71 | | { |
| | 0 | 72 | | genericArgs = typeToConvert.GetGenericArguments(); |
| | 0 | 73 | | converterType = typeof(ImmutableDictionaryOfTKeyTValueConverterWithReflection<,,>); |
| | 0 | 74 | | dictionaryKeyType = genericArgs[0]; |
| | 0 | 75 | | elementType = genericArgs[1]; |
| | 0 | 76 | | } |
| | | 77 | | // IDictionary<TKey, TValue> or deriving from IDictionary<TKey, TValue> |
| | 0 | 78 | | else if ((actualTypeToConvert = typeToConvert.GetCompatibleGenericInterface(typeof(IDictionary<,>))) != null |
| | 0 | 79 | | { |
| | 0 | 80 | | genericArgs = actualTypeToConvert.GetGenericArguments(); |
| | 0 | 81 | | converterType = typeof(IDictionaryOfTKeyTValueConverter<,,>); |
| | 0 | 82 | | dictionaryKeyType = genericArgs[0]; |
| | 0 | 83 | | elementType = genericArgs[1]; |
| | 0 | 84 | | } |
| | | 85 | | // IReadOnlyDictionary<TKey, TValue> or deriving from IReadOnlyDictionary<TKey, TValue> |
| | 0 | 86 | | else if ((actualTypeToConvert = typeToConvert.GetCompatibleGenericInterface(typeof(IReadOnlyDictionary<,>))) |
| | 0 | 87 | | { |
| | 0 | 88 | | genericArgs = actualTypeToConvert.GetGenericArguments(); |
| | 0 | 89 | | converterType = typeof(IReadOnlyDictionaryOfTKeyTValueConverter<,,>); |
| | 0 | 90 | | dictionaryKeyType = genericArgs[0]; |
| | 0 | 91 | | elementType = genericArgs[1]; |
| | 0 | 92 | | } |
| | | 93 | | // Immutable non-dictionaries from System.Collections.Immutable, e.g. ImmutableStack<T> |
| | 0 | 94 | | else if (typeToConvert.IsImmutableEnumerableType()) |
| | 0 | 95 | | { |
| | 0 | 96 | | converterType = typeof(ImmutableEnumerableOfTConverterWithReflection<,>); |
| | 0 | 97 | | elementType = typeToConvert.GetGenericArguments()[0]; |
| | 0 | 98 | | } |
| | | 99 | | // IList<> |
| | 0 | 100 | | else if ((actualTypeToConvert = typeToConvert.GetCompatibleGenericInterface(typeof(IList<>))) != null) |
| | 0 | 101 | | { |
| | 0 | 102 | | converterType = typeof(IListOfTConverter<,>); |
| | 0 | 103 | | elementType = actualTypeToConvert.GetGenericArguments()[0]; |
| | 0 | 104 | | } |
| | | 105 | | // ISet<> |
| | 0 | 106 | | else if ((actualTypeToConvert = typeToConvert.GetCompatibleGenericInterface(typeof(ISet<>))) != null) |
| | 0 | 107 | | { |
| | 0 | 108 | | converterType = typeof(ISetOfTConverter<,>); |
| | 0 | 109 | | elementType = actualTypeToConvert.GetGenericArguments()[0]; |
| | 0 | 110 | | } |
| | | 111 | | #if NET |
| | | 112 | | // IReadOnlySet<> |
| | 0 | 113 | | else if ((actualTypeToConvert = typeToConvert.GetCompatibleGenericInterface(typeof(IReadOnlySet<>))) != null |
| | 0 | 114 | | { |
| | 0 | 115 | | converterType = typeof(IReadOnlySetOfTConverter<,>); |
| | 0 | 116 | | elementType = actualTypeToConvert.GetGenericArguments()[0]; |
| | 0 | 117 | | } |
| | | 118 | | #endif |
| | | 119 | | // ICollection<> |
| | 0 | 120 | | else if ((actualTypeToConvert = typeToConvert.GetCompatibleGenericInterface(typeof(ICollection<>))) != null) |
| | 0 | 121 | | { |
| | 0 | 122 | | converterType = typeof(ICollectionOfTConverter<,>); |
| | 0 | 123 | | elementType = actualTypeToConvert.GetGenericArguments()[0]; |
| | 0 | 124 | | } |
| | | 125 | | // Stack<> or deriving from Stack<> |
| | 0 | 126 | | else if ((actualTypeToConvert = typeToConvert.GetCompatibleGenericBaseClass(typeof(Stack<>))) != null) |
| | 0 | 127 | | { |
| | 0 | 128 | | converterType = typeof(StackOfTConverter<,>); |
| | 0 | 129 | | elementType = actualTypeToConvert.GetGenericArguments()[0]; |
| | 0 | 130 | | } |
| | | 131 | | // Queue<> or deriving from Queue<> |
| | 0 | 132 | | else if ((actualTypeToConvert = typeToConvert.GetCompatibleGenericBaseClass(typeof(Queue<>))) != null) |
| | 0 | 133 | | { |
| | 0 | 134 | | converterType = typeof(QueueOfTConverter<,>); |
| | 0 | 135 | | elementType = actualTypeToConvert.GetGenericArguments()[0]; |
| | 0 | 136 | | } |
| | | 137 | | // ConcurrentStack<> or deriving from ConcurrentStack<> |
| | 0 | 138 | | else if ((actualTypeToConvert = typeToConvert.GetCompatibleGenericBaseClass(typeof(ConcurrentStack<>))) != n |
| | 0 | 139 | | { |
| | 0 | 140 | | converterType = typeof(ConcurrentStackOfTConverter<,>); |
| | 0 | 141 | | elementType = actualTypeToConvert.GetGenericArguments()[0]; |
| | 0 | 142 | | } |
| | | 143 | | // ConcurrentQueue<> or deriving from ConcurrentQueue<> |
| | 0 | 144 | | else if ((actualTypeToConvert = typeToConvert.GetCompatibleGenericBaseClass(typeof(ConcurrentQueue<>))) != n |
| | 0 | 145 | | { |
| | 0 | 146 | | converterType = typeof(ConcurrentQueueOfTConverter<,>); |
| | 0 | 147 | | elementType = actualTypeToConvert.GetGenericArguments()[0]; |
| | 0 | 148 | | } |
| | | 149 | | // IEnumerable<>, types assignable from List<> |
| | 0 | 150 | | else if ((actualTypeToConvert = typeToConvert.GetCompatibleGenericInterface(typeof(IEnumerable<>))) != null) |
| | 0 | 151 | | { |
| | 0 | 152 | | converterType = typeof(IEnumerableOfTConverter<,>); |
| | 0 | 153 | | elementType = actualTypeToConvert.GetGenericArguments()[0]; |
| | 0 | 154 | | } |
| | | 155 | | // Check for non-generics after checking for generics. |
| | 0 | 156 | | else if (typeof(IDictionary).IsAssignableFrom(typeToConvert)) |
| | 0 | 157 | | { |
| | 0 | 158 | | if (typeToConvert == typeof(IDictionary)) |
| | 0 | 159 | | { |
| | 0 | 160 | | return s_converterForIDictionary; |
| | | 161 | | } |
| | | 162 | | |
| | 0 | 163 | | converterType = typeof(IDictionaryConverter<>); |
| | 0 | 164 | | } |
| | 0 | 165 | | else if (typeof(IList).IsAssignableFrom(typeToConvert)) |
| | 0 | 166 | | { |
| | 0 | 167 | | if (typeToConvert == typeof(IList)) |
| | 0 | 168 | | { |
| | 0 | 169 | | return s_converterForIList; |
| | | 170 | | } |
| | | 171 | | |
| | 0 | 172 | | converterType = typeof(IListConverter<>); |
| | 0 | 173 | | } |
| | 0 | 174 | | else if (typeToConvert.IsNonGenericStackOrQueue()) |
| | 0 | 175 | | { |
| | 0 | 176 | | converterType = typeof(StackOrQueueConverterWithReflection<>); |
| | 0 | 177 | | } |
| | | 178 | | else |
| | 0 | 179 | | { |
| | 0 | 180 | | Debug.Assert(typeof(IEnumerable).IsAssignableFrom(typeToConvert)); |
| | 0 | 181 | | if (typeToConvert == typeof(IEnumerable)) |
| | 0 | 182 | | { |
| | 0 | 183 | | return s_converterForIEnumerable; |
| | | 184 | | } |
| | | 185 | | |
| | 0 | 186 | | converterType = typeof(IEnumerableConverter<>); |
| | 0 | 187 | | } |
| | | 188 | | |
| | | 189 | | Type genericType; |
| | 1002 | 190 | | int numberOfGenericArgs = converterType.GetGenericArguments().Length; |
| | 1002 | 191 | | if (numberOfGenericArgs == 1) |
| | 0 | 192 | | { |
| | 0 | 193 | | genericType = converterType.MakeGenericType(typeToConvert); |
| | 0 | 194 | | } |
| | 1002 | 195 | | else if (numberOfGenericArgs == 2) |
| | 638 | 196 | | { |
| | 638 | 197 | | JsonTypeInfo.ValidateType(elementType!); |
| | | 198 | | |
| | 638 | 199 | | genericType = converterType.MakeGenericType(typeToConvert, elementType!); |
| | 638 | 200 | | } |
| | | 201 | | else |
| | 364 | 202 | | { |
| | 364 | 203 | | Debug.Assert(numberOfGenericArgs == 3); |
| | | 204 | | |
| | 364 | 205 | | JsonTypeInfo.ValidateType(elementType!); |
| | 364 | 206 | | JsonTypeInfo.ValidateType(dictionaryKeyType!); |
| | | 207 | | |
| | 364 | 208 | | genericType = converterType.MakeGenericType(typeToConvert, dictionaryKeyType!, elementType!); |
| | 364 | 209 | | } |
| | | 210 | | |
| | 1002 | 211 | | JsonConverter converter = (JsonConverter)Activator.CreateInstance( |
| | 1002 | 212 | | genericType, |
| | 1002 | 213 | | BindingFlags.Instance | BindingFlags.Public, |
| | 1002 | 214 | | binder: null, |
| | 1002 | 215 | | args: null, |
| | 1002 | 216 | | culture: null)!; |
| | | 217 | | |
| | 1002 | 218 | | return converter; |
| | 1002 | 219 | | } |
| | | 220 | | } |
| | | 221 | | } |