3
3
// See the LICENSE file in the project root for more information.
4
4
5
5
using System . Collections ;
6
- using System . Collections . Generic ;
7
6
using System . Diagnostics ;
8
7
using System . Reflection ;
9
- using System . Runtime . CompilerServices ;
10
8
using System . Text . Json . Serialization ;
11
9
using System . Text . Json . Serialization . Converters ;
12
10
@@ -24,7 +22,6 @@ internal abstract class JsonPropertyInfo
24
22
private static readonly JsonDictionaryConverter s_jsonIDictionaryConverter = new DefaultIDictionaryConverter ( ) ;
25
23
private static readonly JsonDictionaryConverter s_jsonImmutableDictionaryConverter = new DefaultImmutableDictionaryConverter ( ) ;
26
24
27
-
28
25
public static readonly JsonPropertyInfo s_missingProperty = new JsonPropertyInfoNotNullable < object , object , object , object > ( ) ;
29
26
30
27
private Type _elementType ;
@@ -141,82 +138,73 @@ private void DetermineSerializationCapabilities()
141
138
{
142
139
if ( HasGetter )
143
140
{
141
+ ShouldSerialize = true ;
142
+
144
143
if ( HasSetter )
145
144
{
146
145
ShouldDeserialize = true ;
147
- }
148
- else if ( ! RuntimePropertyType . IsArray &&
149
- ( typeof ( IList ) . IsAssignableFrom ( RuntimePropertyType ) || typeof ( IDictionary ) . IsAssignableFrom ( RuntimePropertyType ) ) )
150
- {
151
- ShouldDeserialize = true ;
152
- }
153
- }
154
- //else if (HasSetter)
155
- //{
156
- // // todo: Special case where there is no getter but a setter (and an EnumerableConverter)
157
- //}
158
-
159
- if ( ShouldDeserialize )
160
- {
161
- ShouldSerialize = HasGetter ;
162
146
163
- if ( RuntimePropertyType . IsArray )
164
- {
165
- EnumerableConverter = s_jsonArrayConverter ;
166
- }
167
- else if ( ClassType == ClassType . IDictionaryConstructible )
168
- {
169
- // Natively supported type.
170
- if ( DeclaredPropertyType == ImplementedPropertyType )
147
+ if ( RuntimePropertyType . IsArray )
171
148
{
172
- if ( RuntimePropertyType . FullName . StartsWith ( JsonClassInfo . ImmutableNamespaceName ) )
149
+ // Verify that we don't have a multidimensional array.
150
+ if ( RuntimePropertyType . GetArrayRank ( ) > 1 )
173
151
{
174
- DefaultImmutableDictionaryConverter . RegisterImmutableDictionary (
175
- RuntimePropertyType , JsonClassInfo . GetElementType ( RuntimePropertyType , ParentClassType , PropertyInfo , Options ) , Options ) ;
152
+ throw ThrowHelper . GetNotSupportedException_SerializationNotSupportedCollection ( RuntimePropertyType , ParentClassType , PropertyInfo ) ;
153
+ }
176
154
177
- DictionaryConverter = s_jsonImmutableDictionaryConverter ;
155
+ EnumerableConverter = s_jsonArrayConverter ;
156
+ }
157
+ else if ( ClassType == ClassType . IDictionaryConstructible )
158
+ {
159
+ // Natively supported type.
160
+ if ( DeclaredPropertyType == ImplementedPropertyType )
161
+ {
162
+ if ( RuntimePropertyType . FullName . StartsWith ( JsonClassInfo . ImmutableNamespaceName ) )
163
+ {
164
+ DefaultImmutableDictionaryConverter . RegisterImmutableDictionary (
165
+ RuntimePropertyType , JsonClassInfo . GetElementType ( RuntimePropertyType , ParentClassType , PropertyInfo , Options ) , Options ) ;
166
+
167
+ DictionaryConverter = s_jsonImmutableDictionaryConverter ;
168
+ }
169
+ else if ( JsonClassInfo . IsDeserializedByConstructingWithIDictionary ( RuntimePropertyType ) )
170
+ {
171
+ DictionaryConverter = s_jsonIDictionaryConverter ;
172
+ }
178
173
}
179
- else if ( JsonClassInfo . IsDeserializedByConstructingWithIDictionary ( RuntimePropertyType ) )
174
+ // Type that implements a type with ClassType IDictionaryConstructible.
175
+ else
180
176
{
181
- DictionaryConverter = s_jsonIDictionaryConverter ;
177
+ DictionaryConverter = s_jsonDerivedDictionaryConverter ;
182
178
}
183
179
}
184
- // Type that implements a type with ClassType IDictionaryConstructible.
185
- else
186
- {
187
- DictionaryConverter = s_jsonDerivedDictionaryConverter ;
188
- }
189
- }
190
- else if ( ClassType == ClassType . Enumerable )
191
- {
192
- // Else if it's an implementing type that is not assignable from IList.
193
- if ( DeclaredPropertyType != ImplementedPropertyType &&
194
- ( ! typeof ( IList ) . IsAssignableFrom ( DeclaredPropertyType ) ||
195
- ImplementedPropertyType == typeof ( ArrayList ) ||
196
- ImplementedPropertyType == typeof ( IList ) ) )
197
- {
198
- EnumerableConverter = s_jsonDerivedEnumerableConverter ;
199
- }
200
- else if ( JsonClassInfo . IsDeserializedByConstructingWithIList ( RuntimePropertyType ) ||
201
- ( ! typeof ( IList ) . IsAssignableFrom ( RuntimePropertyType ) &&
202
- JsonClassInfo . HasConstructorThatTakesGenericIEnumerable ( RuntimePropertyType , Options ) ) )
180
+ else if ( ClassType == ClassType . Enumerable )
203
181
{
204
- EnumerableConverter = s_jsonICollectionConverter ;
205
- }
206
- else if ( RuntimePropertyType . IsGenericType &&
207
- RuntimePropertyType . FullName . StartsWith ( JsonClassInfo . ImmutableNamespaceName ) &&
208
- RuntimePropertyType . GetGenericArguments ( ) . Length == 1 )
209
- {
210
- DefaultImmutableEnumerableConverter . RegisterImmutableCollection ( RuntimePropertyType ,
211
- JsonClassInfo . GetElementType ( RuntimePropertyType , ParentClassType , PropertyInfo , Options ) , Options ) ;
212
- EnumerableConverter = s_jsonImmutableEnumerableConverter ;
182
+ // Else if it's an implementing type that is not assignable from IList.
183
+ if ( DeclaredPropertyType != ImplementedPropertyType &&
184
+ ( ! typeof ( IList ) . IsAssignableFrom ( DeclaredPropertyType ) ||
185
+ ImplementedPropertyType == typeof ( ArrayList ) ||
186
+ ImplementedPropertyType == typeof ( IList ) ) )
187
+ {
188
+ EnumerableConverter = s_jsonDerivedEnumerableConverter ;
189
+ }
190
+ else if ( JsonClassInfo . IsDeserializedByConstructingWithIList ( RuntimePropertyType ) ||
191
+ ( ! typeof ( IList ) . IsAssignableFrom ( RuntimePropertyType ) &&
192
+ JsonClassInfo . HasConstructorThatTakesGenericIEnumerable ( RuntimePropertyType , Options ) ) )
193
+ {
194
+ EnumerableConverter = s_jsonICollectionConverter ;
195
+ }
196
+ else if ( RuntimePropertyType . IsGenericType &&
197
+ RuntimePropertyType . FullName . StartsWith ( JsonClassInfo . ImmutableNamespaceName ) &&
198
+ RuntimePropertyType . GetGenericArguments ( ) . Length == 1 )
199
+ {
200
+ DefaultImmutableEnumerableConverter . RegisterImmutableCollection ( RuntimePropertyType ,
201
+ JsonClassInfo . GetElementType ( RuntimePropertyType , ParentClassType , PropertyInfo , Options ) , Options ) ;
202
+ EnumerableConverter = s_jsonImmutableEnumerableConverter ;
203
+ }
204
+
213
205
}
214
206
}
215
207
}
216
- else
217
- {
218
- ShouldSerialize = HasGetter && ! Options . IgnoreReadOnlyProperties ;
219
- }
220
208
}
221
209
}
222
210
0 commit comments