| | | 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.Generic; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.Reflection; |
| | | 8 | | using System.Runtime.CompilerServices; |
| | | 9 | | using System.Text.Json.Reflection; |
| | | 10 | | using System.Threading; |
| | | 11 | | |
| | | 12 | | namespace System.Text.Json.Serialization.Metadata |
| | | 13 | | { |
| | | 14 | | public partial class DefaultJsonTypeInfoResolver |
| | | 15 | | { |
| | | 16 | | internal static MemberAccessor MemberAccessor |
| | | 17 | | { |
| | | 18 | | [RequiresUnreferencedCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)] |
| | | 19 | | [RequiresDynamicCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)] |
| | | 20 | | get |
| | 28591 | 21 | | { |
| | 28591 | 22 | | return s_memberAccessor ?? Initialize(); |
| | | 23 | | static MemberAccessor Initialize() |
| | 1 | 24 | | { |
| | 1 | 25 | | MemberAccessor value = |
| | 1 | 26 | | #if NET |
| | 1 | 27 | | // if dynamic code isn't supported, fallback to reflection |
| | 1 | 28 | | RuntimeFeature.IsDynamicCodeSupported ? |
| | 1 | 29 | | new ReflectionEmitCachingMemberAccessor() : |
| | 1 | 30 | | new ReflectionMemberAccessor(); |
| | | 31 | | #elif NETFRAMEWORK |
| | | 32 | | new ReflectionEmitCachingMemberAccessor(); |
| | | 33 | | #else |
| | | 34 | | new ReflectionMemberAccessor(); |
| | | 35 | | #endif |
| | 1 | 36 | | return Interlocked.CompareExchange(ref s_memberAccessor, value, null) ?? value; |
| | 1 | 37 | | } |
| | 28591 | 38 | | } |
| | | 39 | | } |
| | | 40 | | |
| | 0 | 41 | | internal static void ClearMemberAccessorCaches() => s_memberAccessor?.Clear(); |
| | | 42 | | private static MemberAccessor? s_memberAccessor; |
| | | 43 | | |
| | | 44 | | [RequiresUnreferencedCode(JsonSerializer.SerializationUnreferencedCodeMessage)] |
| | | 45 | | [RequiresDynamicCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)] |
| | | 46 | | private static JsonTypeInfo CreateTypeInfoCore(Type type, JsonConverter converter, JsonSerializerOptions options |
| | 12254 | 47 | | { |
| | 12254 | 48 | | JsonTypeInfo typeInfo = JsonTypeInfo.CreateJsonTypeInfo(type, converter, options); |
| | | 49 | | |
| | 12254 | 50 | | if (GetNumberHandlingForType(typeInfo.Type) is { } numberHandling) |
| | 0 | 51 | | { |
| | 0 | 52 | | typeInfo.NumberHandling = numberHandling; |
| | 0 | 53 | | } |
| | | 54 | | |
| | 12254 | 55 | | if (GetObjectCreationHandlingForType(typeInfo.Type) is { } creationHandling) |
| | 0 | 56 | | { |
| | 0 | 57 | | typeInfo.PreferredPropertyObjectCreationHandling = creationHandling; |
| | 0 | 58 | | } |
| | | 59 | | |
| | 12254 | 60 | | if (GetUnmappedMemberHandling(typeInfo.Type) is { } unmappedMemberHandling) |
| | 0 | 61 | | { |
| | 0 | 62 | | typeInfo.UnmappedMemberHandling = unmappedMemberHandling; |
| | 0 | 63 | | } |
| | | 64 | | |
| | 12254 | 65 | | typeInfo.PopulatePolymorphismMetadata(); |
| | 12254 | 66 | | typeInfo.MapInterfaceTypesToCallbacks(); |
| | | 67 | | |
| | 12254 | 68 | | Func<object>? createObject = DetermineCreateObjectDelegate(type, converter); |
| | 12254 | 69 | | typeInfo.SetCreateObjectIfCompatible(createObject); |
| | 12254 | 70 | | typeInfo.CreateObjectForExtensionDataProperty = createObject; |
| | | 71 | | |
| | 12254 | 72 | | if (typeInfo is { Kind: JsonTypeInfoKind.Object, IsNullable: false }) |
| | 1299 | 73 | | { |
| | 1299 | 74 | | NullabilityInfoContext nullabilityCtx = new(); |
| | | 75 | | |
| | 1299 | 76 | | if (converter.ConstructorIsParameterized) |
| | 749 | 77 | | { |
| | | 78 | | // NB parameter metadata must be populated *before* property metadata |
| | | 79 | | // so that properties can be linked to their associated parameters. |
| | 749 | 80 | | PopulateParameterInfoValues(typeInfo, nullabilityCtx); |
| | 749 | 81 | | } |
| | | 82 | | |
| | 1299 | 83 | | PopulateProperties(typeInfo, nullabilityCtx); |
| | | 84 | | |
| | 1299 | 85 | | typeInfo.ConstructorAttributeProvider = typeInfo.Converter.ConstructorInfo; |
| | 1299 | 86 | | } |
| | | 87 | | |
| | | 88 | | // Plug in any converter configuration -- should be run last. |
| | 12254 | 89 | | converter.ConfigureJsonTypeInfo(typeInfo, options); |
| | 12254 | 90 | | converter.ConfigureJsonTypeInfoUsingReflection(typeInfo, options); |
| | 12254 | 91 | | return typeInfo; |
| | 12254 | 92 | | } |
| | | 93 | | |
| | | 94 | | [RequiresUnreferencedCode(JsonSerializer.SerializationUnreferencedCodeMessage)] |
| | | 95 | | [RequiresDynamicCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)] |
| | | 96 | | private static void PopulateProperties(JsonTypeInfo typeInfo, NullabilityInfoContext nullabilityCtx) |
| | 1299 | 97 | | { |
| | 1299 | 98 | | Debug.Assert(!typeInfo.IsReadOnly); |
| | 1299 | 99 | | Debug.Assert(typeInfo.Kind is JsonTypeInfoKind.Object); |
| | | 100 | | |
| | | 101 | | // SetsRequiredMembersAttribute means that all required members are assigned by constructor and therefore th |
| | 1299 | 102 | | bool constructorHasSetsRequiredMembersAttribute = |
| | 1299 | 103 | | typeInfo.Converter.ConstructorInfo?.HasSetsRequiredMembersAttribute() ?? false; |
| | | 104 | | |
| | 1299 | 105 | | JsonTypeInfo.PropertyHierarchyResolutionState state = new(typeInfo.Options); |
| | | 106 | | |
| | | 107 | | // Walk the type hierarchy starting from the current type up to the base type(s) |
| | 7794 | 108 | | foreach (Type currentType in typeInfo.Type.GetSortedTypeHierarchy()) |
| | 2598 | 109 | | { |
| | 2598 | 110 | | if (currentType == JsonTypeInfo.ObjectType || |
| | 2598 | 111 | | currentType == typeof(ValueType)) |
| | 1299 | 112 | | { |
| | | 113 | | // Don't process any members for typeof(object) or System.ValueType |
| | 1299 | 114 | | break; |
| | | 115 | | } |
| | | 116 | | |
| | 1299 | 117 | | AddMembersDeclaredBySuperType( |
| | 1299 | 118 | | typeInfo, |
| | 1299 | 119 | | currentType, |
| | 1299 | 120 | | nullabilityCtx, |
| | 1299 | 121 | | constructorHasSetsRequiredMembersAttribute, |
| | 1299 | 122 | | ref state); |
| | 1299 | 123 | | } |
| | | 124 | | |
| | 1299 | 125 | | if (state.IsPropertyOrderSpecified) |
| | 0 | 126 | | { |
| | 0 | 127 | | typeInfo.PropertyList.SortProperties(); |
| | 0 | 128 | | } |
| | 1299 | 129 | | } |
| | | 130 | | |
| | | 131 | | private const BindingFlags AllInstanceMembers = |
| | | 132 | | BindingFlags.Instance | |
| | | 133 | | BindingFlags.Public | |
| | | 134 | | BindingFlags.NonPublic | |
| | | 135 | | BindingFlags.DeclaredOnly; |
| | | 136 | | |
| | | 137 | | |
| | | 138 | | [RequiresUnreferencedCode(JsonSerializer.SerializationUnreferencedCodeMessage)] |
| | | 139 | | [RequiresDynamicCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)] |
| | | 140 | | private static void AddMembersDeclaredBySuperType( |
| | | 141 | | JsonTypeInfo typeInfo, |
| | | 142 | | Type currentType, |
| | | 143 | | NullabilityInfoContext nullabilityCtx, |
| | | 144 | | bool constructorHasSetsRequiredMembersAttribute, |
| | | 145 | | ref JsonTypeInfo.PropertyHierarchyResolutionState state) |
| | 1299 | 146 | | { |
| | 1299 | 147 | | Debug.Assert(!typeInfo.IsReadOnly); |
| | 1299 | 148 | | Debug.Assert(currentType.IsAssignableFrom(typeInfo.Type)); |
| | | 149 | | |
| | | 150 | | // Compiler adds RequiredMemberAttribute to type if any of the members are marked with 'required' keyword. |
| | 1299 | 151 | | bool shouldCheckMembersForRequiredMemberAttribute = |
| | 1299 | 152 | | !constructorHasSetsRequiredMembersAttribute && currentType.HasRequiredMemberAttribute(); |
| | | 153 | | |
| | 20983 | 154 | | foreach (PropertyInfo propertyInfo in currentType.GetProperties(AllInstanceMembers)) |
| | 8543 | 155 | | { |
| | | 156 | | // Ignore indexers and virtual properties that have overrides that were [JsonIgnore]d. |
| | 8543 | 157 | | if (propertyInfo.GetIndexParameters().Length > 0 || |
| | 8543 | 158 | | PropertyIsOverriddenAndIgnored(propertyInfo, state.IgnoredProperties)) |
| | 0 | 159 | | { |
| | 0 | 160 | | continue; |
| | | 161 | | } |
| | | 162 | | |
| | 8543 | 163 | | bool hasJsonIncludeAttribute = propertyInfo.GetCustomAttribute<JsonIncludeAttribute>(inherit: false) != |
| | | 164 | | |
| | | 165 | | // Only include properties that either have a public getter or a public setter or have the JsonIncludeAt |
| | 8543 | 166 | | if (propertyInfo.GetMethod?.IsPublic == true || |
| | 8543 | 167 | | propertyInfo.SetMethod?.IsPublic == true || |
| | 8543 | 168 | | hasJsonIncludeAttribute) |
| | 7794 | 169 | | { |
| | 7794 | 170 | | AddMember( |
| | 7794 | 171 | | typeInfo, |
| | 7794 | 172 | | typeToConvert: propertyInfo.PropertyType, |
| | 7794 | 173 | | memberInfo: propertyInfo, |
| | 7794 | 174 | | nullabilityCtx, |
| | 7794 | 175 | | shouldCheckMembersForRequiredMemberAttribute, |
| | 7794 | 176 | | hasJsonIncludeAttribute, |
| | 7794 | 177 | | ref state); |
| | 7794 | 178 | | } |
| | 8543 | 179 | | } |
| | | 180 | | |
| | 19485 | 181 | | foreach (FieldInfo fieldInfo in currentType.GetFields(AllInstanceMembers)) |
| | 7794 | 182 | | { |
| | 7794 | 183 | | bool hasJsonIncludeAttribute = fieldInfo.GetCustomAttribute<JsonIncludeAttribute>(inherit: false) != nul |
| | 7794 | 184 | | if (hasJsonIncludeAttribute || (fieldInfo.IsPublic && typeInfo.Options.IncludeFields)) |
| | 0 | 185 | | { |
| | 0 | 186 | | AddMember( |
| | 0 | 187 | | typeInfo, |
| | 0 | 188 | | typeToConvert: fieldInfo.FieldType, |
| | 0 | 189 | | memberInfo: fieldInfo, |
| | 0 | 190 | | nullabilityCtx, |
| | 0 | 191 | | shouldCheckMembersForRequiredMemberAttribute, |
| | 0 | 192 | | hasJsonIncludeAttribute, |
| | 0 | 193 | | ref state); |
| | 0 | 194 | | } |
| | 7794 | 195 | | } |
| | 1299 | 196 | | } |
| | | 197 | | |
| | | 198 | | [RequiresUnreferencedCode(JsonSerializer.SerializationUnreferencedCodeMessage)] |
| | | 199 | | [RequiresDynamicCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)] |
| | | 200 | | private static void AddMember( |
| | | 201 | | JsonTypeInfo typeInfo, |
| | | 202 | | Type typeToConvert, |
| | | 203 | | MemberInfo memberInfo, |
| | | 204 | | NullabilityInfoContext nullabilityCtx, |
| | | 205 | | bool shouldCheckForRequiredKeyword, |
| | | 206 | | bool hasJsonIncludeAttribute, |
| | | 207 | | ref JsonTypeInfo.PropertyHierarchyResolutionState state) |
| | 7794 | 208 | | { |
| | 7794 | 209 | | JsonPropertyInfo? jsonPropertyInfo = CreatePropertyInfo(typeInfo, typeToConvert, memberInfo, nullabilityCtx, |
| | 7794 | 210 | | if (jsonPropertyInfo == null) |
| | 0 | 211 | | { |
| | | 212 | | // ignored invalid property |
| | 0 | 213 | | return; |
| | | 214 | | } |
| | | 215 | | |
| | 7794 | 216 | | Debug.Assert(jsonPropertyInfo.Name != null); |
| | 7794 | 217 | | typeInfo.PropertyList.AddPropertyWithConflictResolution(jsonPropertyInfo, ref state); |
| | 7794 | 218 | | } |
| | | 219 | | |
| | | 220 | | [RequiresUnreferencedCode(JsonSerializer.SerializationUnreferencedCodeMessage)] |
| | | 221 | | [RequiresDynamicCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)] |
| | | 222 | | private static JsonPropertyInfo? CreatePropertyInfo( |
| | | 223 | | JsonTypeInfo typeInfo, |
| | | 224 | | Type typeToConvert, |
| | | 225 | | MemberInfo memberInfo, |
| | | 226 | | NullabilityInfoContext nullabilityCtx, |
| | | 227 | | JsonSerializerOptions options, |
| | | 228 | | bool shouldCheckForRequiredKeyword, |
| | | 229 | | bool hasJsonIncludeAttribute) |
| | 7794 | 230 | | { |
| | 7794 | 231 | | JsonIgnoreCondition? ignoreCondition = memberInfo.GetCustomAttribute<JsonIgnoreAttribute>(inherit: false)?.C |
| | | 232 | | |
| | 7794 | 233 | | if (JsonTypeInfo.IsInvalidForSerialization(typeToConvert)) |
| | 0 | 234 | | { |
| | 0 | 235 | | if (ignoreCondition == JsonIgnoreCondition.Always) |
| | 0 | 236 | | return null; |
| | | 237 | | |
| | 0 | 238 | | ThrowHelper.ThrowInvalidOperationException_CannotSerializeInvalidType(typeToConvert, memberInfo.Declarin |
| | | 239 | | } |
| | | 240 | | |
| | | 241 | | // Resolve any custom converters on the attribute level. |
| | | 242 | | JsonConverter? customConverter; |
| | | 243 | | try |
| | 7794 | 244 | | { |
| | 7794 | 245 | | customConverter = GetCustomConverterForMember(typeToConvert, memberInfo, options); |
| | 7794 | 246 | | } |
| | 0 | 247 | | catch (InvalidOperationException) when (ignoreCondition == JsonIgnoreCondition.Always) |
| | 0 | 248 | | { |
| | | 249 | | // skip property altogether if attribute is invalid and the property is ignored |
| | 0 | 250 | | return null; |
| | | 251 | | } |
| | | 252 | | |
| | 7794 | 253 | | JsonPropertyInfo jsonPropertyInfo = typeInfo.CreatePropertyUsingReflection(typeToConvert, declaringType: mem |
| | 7794 | 254 | | PopulatePropertyInfo(jsonPropertyInfo, memberInfo, customConverter, ignoreCondition, nullabilityCtx, shouldC |
| | 7794 | 255 | | return jsonPropertyInfo; |
| | 7794 | 256 | | } |
| | | 257 | | |
| | | 258 | | private static JsonNumberHandling? GetNumberHandlingForType(Type type) |
| | 12254 | 259 | | { |
| | 12254 | 260 | | JsonNumberHandlingAttribute? numberHandlingAttribute = type.GetUniqueCustomAttribute<JsonNumberHandlingAttri |
| | 12254 | 261 | | return numberHandlingAttribute?.Handling; |
| | 12254 | 262 | | } |
| | | 263 | | |
| | | 264 | | private static JsonObjectCreationHandling? GetObjectCreationHandlingForType(Type type) |
| | 12254 | 265 | | { |
| | 12254 | 266 | | JsonObjectCreationHandlingAttribute? creationHandlingAttribute = type.GetUniqueCustomAttribute<JsonObjectCre |
| | 12254 | 267 | | return creationHandlingAttribute?.Handling; |
| | 12254 | 268 | | } |
| | | 269 | | |
| | | 270 | | private static JsonUnmappedMemberHandling? GetUnmappedMemberHandling(Type type) |
| | 12254 | 271 | | { |
| | 12254 | 272 | | JsonUnmappedMemberHandlingAttribute? numberHandlingAttribute = type.GetUniqueCustomAttribute<JsonUnmappedMem |
| | 12254 | 273 | | return numberHandlingAttribute?.UnmappedMemberHandling; |
| | 12254 | 274 | | } |
| | | 275 | | |
| | | 276 | | private static bool PropertyIsOverriddenAndIgnored(PropertyInfo propertyInfo, Dictionary<string, JsonPropertyInf |
| | 8543 | 277 | | { |
| | 8543 | 278 | | return propertyInfo.IsVirtual() && |
| | 8543 | 279 | | ignoredMembers?.TryGetValue(propertyInfo.Name, out JsonPropertyInfo? ignoredMember) == true && |
| | 8543 | 280 | | ignoredMember.IsVirtual && |
| | 8543 | 281 | | propertyInfo.PropertyType == ignoredMember.PropertyType; |
| | 8543 | 282 | | } |
| | | 283 | | |
| | | 284 | | [RequiresUnreferencedCode(JsonSerializer.SerializationUnreferencedCodeMessage)] |
| | | 285 | | [RequiresDynamicCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)] |
| | | 286 | | private static void PopulateParameterInfoValues(JsonTypeInfo typeInfo, NullabilityInfoContext nullabilityCtx) |
| | 749 | 287 | | { |
| | 749 | 288 | | Debug.Assert(typeInfo.Converter.ConstructorInfo != null); |
| | 749 | 289 | | ParameterInfo[] parameters = typeInfo.Converter.ConstructorInfo.GetParameters(); |
| | 749 | 290 | | int parameterCount = parameters.Length; |
| | 749 | 291 | | JsonParameterInfoValues[] jsonParameters = new JsonParameterInfoValues[parameterCount]; |
| | | 292 | | |
| | 10486 | 293 | | for (int i = 0; i < parameterCount; i++) |
| | 4494 | 294 | | { |
| | 4494 | 295 | | ParameterInfo reflectionInfo = parameters[i]; |
| | | 296 | | |
| | | 297 | | // Trimmed parameter names are reported as null in CoreCLR or "" in Mono. |
| | 4494 | 298 | | if (string.IsNullOrEmpty(reflectionInfo.Name)) |
| | 0 | 299 | | { |
| | 0 | 300 | | Debug.Assert(typeInfo.Converter.ConstructorInfo.DeclaringType != null); |
| | 0 | 301 | | ThrowHelper.ThrowNotSupportedException_ConstructorContainsNullParameterNames(typeInfo.Converter.Cons |
| | | 302 | | } |
| | | 303 | | |
| | 4494 | 304 | | JsonParameterInfoValues jsonInfo = new() |
| | 4494 | 305 | | { |
| | 4494 | 306 | | Name = reflectionInfo.Name, |
| | 4494 | 307 | | ParameterType = reflectionInfo.ParameterType, |
| | 4494 | 308 | | Position = reflectionInfo.Position, |
| | 4494 | 309 | | HasDefaultValue = reflectionInfo.HasDefaultValue, |
| | 4494 | 310 | | DefaultValue = reflectionInfo.GetDefaultValue(), |
| | 4494 | 311 | | IsNullable = DetermineParameterNullability(reflectionInfo, nullabilityCtx) is not NullabilityState.N |
| | 4494 | 312 | | }; |
| | | 313 | | |
| | 4494 | 314 | | jsonParameters[i] = jsonInfo; |
| | 4494 | 315 | | } |
| | | 316 | | |
| | 749 | 317 | | typeInfo.PopulateParameterInfoValues(jsonParameters); |
| | 749 | 318 | | } |
| | | 319 | | |
| | | 320 | | [RequiresUnreferencedCode(JsonSerializer.SerializationUnreferencedCodeMessage)] |
| | | 321 | | [RequiresDynamicCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)] |
| | | 322 | | private static void PopulatePropertyInfo( |
| | | 323 | | JsonPropertyInfo jsonPropertyInfo, |
| | | 324 | | MemberInfo memberInfo, |
| | | 325 | | JsonConverter? customConverter, |
| | | 326 | | JsonIgnoreCondition? ignoreCondition, |
| | | 327 | | NullabilityInfoContext nullabilityCtx, |
| | | 328 | | bool shouldCheckForRequiredKeyword, |
| | | 329 | | bool hasJsonIncludeAttribute) |
| | 7794 | 330 | | { |
| | 7794 | 331 | | Debug.Assert(jsonPropertyInfo.AttributeProvider == null); |
| | | 332 | | |
| | 7794 | 333 | | switch (jsonPropertyInfo.AttributeProvider = memberInfo) |
| | | 334 | | { |
| | | 335 | | case PropertyInfo propertyInfo: |
| | 7794 | 336 | | jsonPropertyInfo.MemberName = propertyInfo.Name; |
| | 7794 | 337 | | jsonPropertyInfo.IsVirtual = propertyInfo.IsVirtual(); |
| | 7794 | 338 | | jsonPropertyInfo.MemberType = MemberTypes.Property; |
| | 7794 | 339 | | break; |
| | | 340 | | case FieldInfo fieldInfo: |
| | 0 | 341 | | jsonPropertyInfo.MemberName = fieldInfo.Name; |
| | 0 | 342 | | jsonPropertyInfo.MemberType = MemberTypes.Field; |
| | 0 | 343 | | break; |
| | | 344 | | default: |
| | 0 | 345 | | Debug.Fail("Only FieldInfo and PropertyInfo members are supported."); |
| | | 346 | | break; |
| | | 347 | | } |
| | | 348 | | |
| | 7794 | 349 | | jsonPropertyInfo.CustomConverter = customConverter; |
| | 7794 | 350 | | DeterminePropertyPolicies(jsonPropertyInfo, memberInfo); |
| | 7794 | 351 | | DeterminePropertyName(jsonPropertyInfo, memberInfo); |
| | 7794 | 352 | | DeterminePropertyIsRequired(jsonPropertyInfo, memberInfo, shouldCheckForRequiredKeyword); |
| | 7794 | 353 | | DeterminePropertyNullability(jsonPropertyInfo, memberInfo, nullabilityCtx); |
| | | 354 | | |
| | 7794 | 355 | | if (ignoreCondition != JsonIgnoreCondition.Always) |
| | 7794 | 356 | | { |
| | 7794 | 357 | | jsonPropertyInfo.DetermineReflectionPropertyAccessors(memberInfo, useNonPublicAccessors: hasJsonIncludeA |
| | 7794 | 358 | | } |
| | | 359 | | |
| | 7794 | 360 | | jsonPropertyInfo.IgnoreCondition = ignoreCondition; |
| | 7794 | 361 | | jsonPropertyInfo.IsExtensionData = memberInfo.GetCustomAttribute<JsonExtensionDataAttribute>(inherit: false) |
| | 7794 | 362 | | } |
| | | 363 | | |
| | | 364 | | private static void DeterminePropertyPolicies(JsonPropertyInfo propertyInfo, MemberInfo memberInfo) |
| | 7794 | 365 | | { |
| | 7794 | 366 | | JsonPropertyOrderAttribute? orderAttr = memberInfo.GetCustomAttribute<JsonPropertyOrderAttribute>(inherit: f |
| | 7794 | 367 | | propertyInfo.Order = orderAttr?.Order ?? 0; |
| | | 368 | | |
| | 7794 | 369 | | JsonNumberHandlingAttribute? numberHandlingAttr = memberInfo.GetCustomAttribute<JsonNumberHandlingAttribute> |
| | 7794 | 370 | | propertyInfo.NumberHandling = numberHandlingAttr?.Handling; |
| | | 371 | | |
| | 7794 | 372 | | JsonObjectCreationHandlingAttribute? objectCreationHandlingAttr = memberInfo.GetCustomAttribute<JsonObjectCr |
| | 7794 | 373 | | propertyInfo.ObjectCreationHandling = objectCreationHandlingAttr?.Handling; |
| | 7794 | 374 | | } |
| | | 375 | | |
| | | 376 | | private static void DeterminePropertyName(JsonPropertyInfo propertyInfo, MemberInfo memberInfo) |
| | 7794 | 377 | | { |
| | 7794 | 378 | | JsonPropertyNameAttribute? nameAttribute = memberInfo.GetCustomAttribute<JsonPropertyNameAttribute>(inherit: |
| | | 379 | | string? name; |
| | 7794 | 380 | | if (nameAttribute != null) |
| | 0 | 381 | | { |
| | 0 | 382 | | name = nameAttribute.Name; |
| | 0 | 383 | | } |
| | 7794 | 384 | | else if (propertyInfo.Options.PropertyNamingPolicy != null) |
| | 0 | 385 | | { |
| | 0 | 386 | | name = propertyInfo.Options.PropertyNamingPolicy.ConvertName(memberInfo.Name); |
| | 0 | 387 | | } |
| | | 388 | | else |
| | 7794 | 389 | | { |
| | 7794 | 390 | | name = memberInfo.Name; |
| | 7794 | 391 | | } |
| | | 392 | | |
| | 7794 | 393 | | if (name == null) |
| | 0 | 394 | | { |
| | 0 | 395 | | ThrowHelper.ThrowInvalidOperationException_SerializerPropertyNameNull(propertyInfo); |
| | | 396 | | } |
| | | 397 | | |
| | 7794 | 398 | | propertyInfo.Name = name; |
| | 7794 | 399 | | } |
| | | 400 | | |
| | | 401 | | private static void DeterminePropertyIsRequired(JsonPropertyInfo propertyInfo, MemberInfo memberInfo, bool shoul |
| | 7794 | 402 | | { |
| | 7794 | 403 | | propertyInfo.IsRequired = |
| | 7794 | 404 | | memberInfo.GetCustomAttribute<JsonRequiredAttribute>(inherit: false) != null |
| | 7794 | 405 | | || (shouldCheckForRequiredKeyword && memberInfo.HasRequiredMemberAttribute()); |
| | 7794 | 406 | | } |
| | | 407 | | |
| | | 408 | | [RequiresUnreferencedCode(JsonSerializer.SerializationUnreferencedCodeMessage)] |
| | | 409 | | [RequiresDynamicCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)] |
| | | 410 | | internal static void DeterminePropertyAccessors<T>(JsonPropertyInfo<T> jsonPropertyInfo, MemberInfo memberInfo, |
| | 7794 | 411 | | { |
| | 7794 | 412 | | Debug.Assert(memberInfo is FieldInfo or PropertyInfo); |
| | | 413 | | |
| | 7794 | 414 | | switch (memberInfo) |
| | | 415 | | { |
| | | 416 | | case PropertyInfo propertyInfo: |
| | 7794 | 417 | | MethodInfo? getMethod = propertyInfo.GetMethod; |
| | 7794 | 418 | | if (getMethod != null && (getMethod.IsPublic || useNonPublicAccessors)) |
| | 7794 | 419 | | { |
| | 7794 | 420 | | jsonPropertyInfo.Get = MemberAccessor.CreatePropertyGetter<T>(propertyInfo); |
| | 7794 | 421 | | } |
| | | 422 | | |
| | 7794 | 423 | | MethodInfo? setMethod = propertyInfo.SetMethod; |
| | 7794 | 424 | | if (setMethod != null && (setMethod.IsPublic || useNonPublicAccessors)) |
| | 7794 | 425 | | { |
| | 7794 | 426 | | jsonPropertyInfo.Set = MemberAccessor.CreatePropertySetter<T>(propertyInfo); |
| | 7794 | 427 | | } |
| | | 428 | | |
| | 7794 | 429 | | break; |
| | | 430 | | |
| | | 431 | | case FieldInfo fieldInfo: |
| | 0 | 432 | | Debug.Assert(fieldInfo.IsPublic || useNonPublicAccessors); |
| | | 433 | | |
| | 0 | 434 | | jsonPropertyInfo.Get = MemberAccessor.CreateFieldGetter<T>(fieldInfo); |
| | | 435 | | |
| | 0 | 436 | | if (!fieldInfo.IsInitOnly) |
| | 0 | 437 | | { |
| | 0 | 438 | | jsonPropertyInfo.Set = MemberAccessor.CreateFieldSetter<T>(fieldInfo); |
| | 0 | 439 | | } |
| | | 440 | | |
| | 0 | 441 | | break; |
| | | 442 | | |
| | | 443 | | default: |
| | 0 | 444 | | Debug.Fail($"Invalid MemberInfo type: {memberInfo.MemberType}"); |
| | | 445 | | break; |
| | | 446 | | } |
| | 7794 | 447 | | } |
| | | 448 | | |
| | | 449 | | [RequiresUnreferencedCode(JsonSerializer.SerializationUnreferencedCodeMessage)] |
| | | 450 | | [RequiresDynamicCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)] |
| | | 451 | | private static Func<object>? DetermineCreateObjectDelegate(Type type, JsonConverter converter) |
| | 12254 | 452 | | { |
| | 12254 | 453 | | ConstructorInfo? defaultCtor = null; |
| | | 454 | | |
| | 12254 | 455 | | if (converter.ConstructorInfo != null && !converter.ConstructorIsParameterized) |
| | 550 | 456 | | { |
| | | 457 | | // A parameterless constructor has been resolved by the converter |
| | | 458 | | // (e.g. it might be a non-public ctor with JsonConstructorAttribute). |
| | 550 | 459 | | defaultCtor = converter.ConstructorInfo; |
| | 550 | 460 | | } |
| | | 461 | | |
| | | 462 | | // Fall back to resolving any public constructors on the type. |
| | 12254 | 463 | | defaultCtor ??= type.GetConstructor(BindingFlags.Public | BindingFlags.Instance, binder: null, Type.EmptyTyp |
| | | 464 | | |
| | 12254 | 465 | | return MemberAccessor.CreateParameterlessConstructor(type, defaultCtor); |
| | 12254 | 466 | | } |
| | | 467 | | |
| | | 468 | | [RequiresUnreferencedCode(JsonSerializer.SerializationUnreferencedCodeMessage)] |
| | | 469 | | [RequiresDynamicCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)] |
| | | 470 | | private static void DeterminePropertyNullability(JsonPropertyInfo propertyInfo, MemberInfo memberInfo, Nullabili |
| | 7794 | 471 | | { |
| | 7794 | 472 | | if (!propertyInfo.PropertyTypeCanBeNull) |
| | 5134 | 473 | | { |
| | 5134 | 474 | | return; |
| | | 475 | | } |
| | | 476 | | |
| | | 477 | | NullabilityInfo nullabilityInfo; |
| | 2660 | 478 | | if (propertyInfo.MemberType is MemberTypes.Property) |
| | 2660 | 479 | | { |
| | 2660 | 480 | | nullabilityInfo = nullabilityCtx.Create((PropertyInfo)memberInfo); |
| | 2660 | 481 | | } |
| | | 482 | | else |
| | 0 | 483 | | { |
| | 0 | 484 | | Debug.Assert(propertyInfo.MemberType is MemberTypes.Field); |
| | 0 | 485 | | nullabilityInfo = nullabilityCtx.Create((FieldInfo)memberInfo); |
| | 0 | 486 | | } |
| | | 487 | | |
| | 2660 | 488 | | propertyInfo.IsGetNullable = nullabilityInfo.ReadState is not NullabilityState.NotNull; |
| | 2660 | 489 | | propertyInfo.IsSetNullable = nullabilityInfo.WriteState is not NullabilityState.NotNull; |
| | 7794 | 490 | | } |
| | | 491 | | |
| | | 492 | | [RequiresUnreferencedCode(JsonSerializer.SerializationUnreferencedCodeMessage)] |
| | | 493 | | [RequiresDynamicCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)] |
| | | 494 | | private static NullabilityState DetermineParameterNullability(ParameterInfo parameterInfo, NullabilityInfoContex |
| | 4494 | 495 | | { |
| | 4494 | 496 | | if (!parameterInfo.ParameterType.IsNullableType()) |
| | 2909 | 497 | | { |
| | 2909 | 498 | | return NullabilityState.NotNull; |
| | | 499 | | } |
| | | 500 | | #if NET8_0 |
| | | 501 | | // Workaround for https://github.com/dotnet/runtime/issues/92487 |
| | | 502 | | // The fix has been incorporated into .NET 9 (and the polyfilled implementations in netfx). |
| | | 503 | | // Should be removed once .NET 8 support is dropped. |
| | | 504 | | if (parameterInfo.GetGenericParameterDefinition() is { ParameterType: { IsGenericParameter: true } typeParam |
| | | 505 | | { |
| | | 506 | | // Step 1. Look for nullable annotations on the type parameter. |
| | | 507 | | if (GetNullableFlags(typeParam) is byte[] flags) |
| | | 508 | | { |
| | | 509 | | return TranslateByte(flags[0]); |
| | | 510 | | } |
| | | 511 | | |
| | | 512 | | // Step 2. Look for nullable annotations on the generic method declaration. |
| | | 513 | | if (typeParam.DeclaringMethod != null && GetNullableContextFlag(typeParam.DeclaringMethod) is byte flag) |
| | | 514 | | { |
| | | 515 | | return TranslateByte(flag); |
| | | 516 | | } |
| | | 517 | | |
| | | 518 | | // Step 3. Look for nullable annotations on the generic type declaration. |
| | | 519 | | if (GetNullableContextFlag(typeParam.DeclaringType!) is byte flag2) |
| | | 520 | | { |
| | | 521 | | return TranslateByte(flag2); |
| | | 522 | | } |
| | | 523 | | |
| | | 524 | | // Default to nullable. |
| | | 525 | | return NullabilityState.Nullable; |
| | | 526 | | |
| | | 527 | | static byte[]? GetNullableFlags(MemberInfo member) |
| | | 528 | | { |
| | | 529 | | foreach (CustomAttributeData attr in member.GetCustomAttributesData()) |
| | | 530 | | { |
| | | 531 | | Type attrType = attr.AttributeType; |
| | | 532 | | if (attrType.Name == "NullableAttribute" && attrType.Namespace == "System.Runtime.CompilerServic |
| | | 533 | | { |
| | | 534 | | foreach (CustomAttributeTypedArgument ctorArg in attr.ConstructorArguments) |
| | | 535 | | { |
| | | 536 | | switch (ctorArg.Value) |
| | | 537 | | { |
| | | 538 | | case byte flag: |
| | | 539 | | return [flag]; |
| | | 540 | | case byte[] flags: |
| | | 541 | | return flags; |
| | | 542 | | } |
| | | 543 | | } |
| | | 544 | | } |
| | | 545 | | } |
| | | 546 | | |
| | | 547 | | return null; |
| | | 548 | | } |
| | | 549 | | |
| | | 550 | | static byte? GetNullableContextFlag(MemberInfo member) |
| | | 551 | | { |
| | | 552 | | foreach (CustomAttributeData attr in member.GetCustomAttributesData()) |
| | | 553 | | { |
| | | 554 | | Type attrType = attr.AttributeType; |
| | | 555 | | if (attrType.Name == "NullableContextAttribute" && attrType.Namespace == "System.Runtime.Compile |
| | | 556 | | { |
| | | 557 | | foreach (CustomAttributeTypedArgument ctorArg in attr.ConstructorArguments) |
| | | 558 | | { |
| | | 559 | | if (ctorArg.Value is byte flag) |
| | | 560 | | { |
| | | 561 | | return flag; |
| | | 562 | | } |
| | | 563 | | } |
| | | 564 | | } |
| | | 565 | | } |
| | | 566 | | |
| | | 567 | | return null; |
| | | 568 | | } |
| | | 569 | | |
| | | 570 | | static NullabilityState TranslateByte(byte b) => |
| | | 571 | | b switch |
| | | 572 | | { |
| | | 573 | | 1 => NullabilityState.NotNull, |
| | | 574 | | 2 => NullabilityState.Nullable, |
| | | 575 | | _ => NullabilityState.Unknown |
| | | 576 | | }; |
| | | 577 | | } |
| | | 578 | | #endif |
| | 1585 | 579 | | NullabilityInfo nullability = nullabilityCtx.Create(parameterInfo); |
| | 1585 | 580 | | return nullability.WriteState; |
| | 4494 | 581 | | } |
| | | 582 | | } |
| | | 583 | | } |