| | | 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.Diagnostics; |
| | | 5 | | using System.Diagnostics.CodeAnalysis; |
| | | 6 | | using System.Reflection; |
| | | 7 | | |
| | | 8 | | namespace System.Text.Json.Serialization.Metadata |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Provides JSON serialization-related metadata about a constructor parameter. |
| | | 12 | | /// </summary> |
| | | 13 | | public abstract class JsonParameterInfo |
| | | 14 | | { |
| | 4494 | 15 | | internal JsonParameterInfo(JsonParameterInfoValues parameterInfoValues, JsonPropertyInfo matchingProperty) |
| | 4494 | 16 | | { |
| | 4494 | 17 | | Debug.Assert(matchingProperty.PropertyType == parameterInfoValues.ParameterType); |
| | | 18 | | |
| | 4494 | 19 | | Position = parameterInfoValues.Position; |
| | 4494 | 20 | | Name = parameterInfoValues.Name; |
| | 4494 | 21 | | HasDefaultValue = parameterInfoValues.HasDefaultValue; |
| | 4494 | 22 | | DefaultValue = parameterInfoValues.HasDefaultValue ? parameterInfoValues.DefaultValue : null; |
| | 4494 | 23 | | MatchingProperty = matchingProperty; |
| | 4494 | 24 | | IsMemberInitializer = parameterInfoValues.IsMemberInitializer; |
| | 4494 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Gets the declaring type of the constructor. |
| | | 29 | | /// </summary> |
| | 0 | 30 | | public Type DeclaringType => MatchingProperty.DeclaringType; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Gets the zero-based position of the parameter in the formal parameter list. |
| | | 34 | | /// </summary> |
| | 48 | 35 | | public int Position { get; } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Gets the type of this parameter. |
| | | 39 | | /// </summary> |
| | 0 | 40 | | public Type ParameterType => MatchingProperty.PropertyType; |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Gets the name of the parameter. |
| | | 44 | | /// </summary> |
| | 0 | 45 | | public string Name { get; } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Gets a value indicating whether the parameter has a default value. |
| | | 49 | | /// </summary> |
| | 0 | 50 | | public bool HasDefaultValue { get; } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Gets a value indicating the default value if the parameter has a default value. |
| | | 54 | | /// </summary> |
| | 0 | 55 | | public object? DefaultValue { get; } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// The default value to be passed to the constructor argument array, replacing null with default(TParameter). |
| | | 59 | | /// </summary> |
| | 4542 | 60 | | internal object? EffectiveDefaultValue { get; private protected init; } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Gets a value indicating whether the constructor parameter is annotated as nullable. |
| | | 64 | | /// </summary> |
| | | 65 | | /// <remarks> |
| | | 66 | | /// Contracts originating from <see cref="DefaultJsonTypeInfoResolver"/> or <see cref="JsonSerializerContext"/>, |
| | | 67 | | /// derive the value of this parameter from nullable reference type annotations, including annotations |
| | | 68 | | /// from attributes such as <see cref="AllowNullAttribute"/> or <see cref="DisallowNullAttribute"/>. |
| | | 69 | | /// |
| | | 70 | | /// This property has no effect on deserialization unless the <see cref="JsonSerializerOptions.RespectNullableAn |
| | | 71 | | /// property has been enabled, in which case the serializer will reject any <see langword="null"/> deserializati |
| | | 72 | | /// |
| | | 73 | | /// This setting is in sync with the associated <see cref="JsonPropertyInfo.IsSetNullable"/> property. |
| | | 74 | | /// </remarks> |
| | 0 | 75 | | public bool IsNullable => MatchingProperty.IsSetNullable; |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Gets a value indicating whether the parameter represents a required or init-only member initializer. |
| | | 79 | | /// </summary> |
| | | 80 | | /// <remarks> |
| | | 81 | | /// Only returns <see langword="true" /> for source generated metadata which can only access |
| | | 82 | | /// required or init-only member initializers using object initialize expressions. |
| | | 83 | | /// </remarks> |
| | 0 | 84 | | public bool IsMemberInitializer { get; } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Gets a custom attribute provider for the current parameter. |
| | | 88 | | /// </summary> |
| | | 89 | | /// <remarks> |
| | | 90 | | /// When resolving metadata via the built-in resolvers this will be populated with |
| | | 91 | | /// the underlying <see cref="ParameterInfo" /> of the constructor metadata. |
| | | 92 | | /// </remarks> |
| | | 93 | | public ICustomAttributeProvider? AttributeProvider |
| | | 94 | | { |
| | | 95 | | get |
| | 0 | 96 | | { |
| | | 97 | | // Use delayed initialization to ensure that reflection dependencies are pay-for-play. |
| | 0 | 98 | | Debug.Assert(MatchingProperty.DeclaringTypeInfo != null, "Declaring type metadata must have already been |
| | 0 | 99 | | ICustomAttributeProvider? parameterInfo = _attributeProvider; |
| | 0 | 100 | | if (parameterInfo is null && MatchingProperty.DeclaringTypeInfo.ConstructorAttributeProvider is MethodBa |
| | 0 | 101 | | { |
| | 0 | 102 | | ParameterInfo[] parameters = ctorInfo.GetParameters(); |
| | 0 | 103 | | if (Position < parameters.Length) |
| | 0 | 104 | | { |
| | 0 | 105 | | _attributeProvider = parameterInfo = parameters[Position]; |
| | 0 | 106 | | } |
| | 0 | 107 | | } |
| | | 108 | | |
| | 0 | 109 | | return parameterInfo; |
| | 0 | 110 | | } |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | private ICustomAttributeProvider? _attributeProvider; |
| | | 114 | | |
| | 0 | 115 | | internal JsonPropertyInfo MatchingProperty { get; } |
| | | 116 | | |
| | 0 | 117 | | internal JsonConverter EffectiveConverter => MatchingProperty.EffectiveConverter; |
| | 0 | 118 | | internal bool IgnoreNullTokensOnRead => MatchingProperty.IgnoreNullTokensOnRead; |
| | 0 | 119 | | internal JsonSerializerOptions Options => MatchingProperty.Options; |
| | | 120 | | |
| | | 121 | | // The effective name of the parameter as UTF-8 bytes. |
| | 0 | 122 | | internal byte[] JsonNameAsUtf8Bytes => MatchingProperty.NameAsUtf8Bytes; |
| | 0 | 123 | | internal JsonNumberHandling? NumberHandling => MatchingProperty.EffectiveNumberHandling; |
| | 0 | 124 | | internal JsonTypeInfo JsonTypeInfo => MatchingProperty.JsonTypeInfo; |
| | 0 | 125 | | internal bool ShouldDeserialize => !MatchingProperty.IsIgnored; |
| | 0 | 126 | | internal bool IsRequiredParameter => !HasDefaultValue && !IsMemberInitializer; |
| | | 127 | | } |
| | | 128 | | } |