| | | 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.Threading; |
| | | 9 | | |
| | | 10 | | namespace System.Text.Json.Serialization.Metadata |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Provides JSON serialization-related metadata about a property or field defined in an object. |
| | | 14 | | /// </summary> |
| | | 15 | | [DebuggerDisplay("{DebuggerDisplay,nq}")] |
| | | 16 | | public abstract class JsonPropertyInfo |
| | | 17 | | { |
| | 0 | 18 | | internal static readonly JsonPropertyInfo s_missingProperty = GetPropertyPlaceholder(); |
| | | 19 | | |
| | 262285 | 20 | | internal JsonTypeInfo? DeclaringTypeInfo { get; private set; } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Converter after applying CustomConverter (i.e. JsonConverterAttribute) |
| | | 24 | | /// </summary> |
| | | 25 | | internal JsonConverter EffectiveConverter |
| | | 26 | | { |
| | | 27 | | get |
| | 132474 | 28 | | { |
| | 132474 | 29 | | Debug.Assert(_effectiveConverter != null); |
| | 132474 | 30 | | return _effectiveConverter; |
| | 132474 | 31 | | } |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | private protected JsonConverter? _effectiveConverter; |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Gets or sets a custom converter override for the current property. |
| | | 38 | | /// </summary> |
| | | 39 | | /// <exception cref="InvalidOperationException"> |
| | | 40 | | /// The <see cref="JsonPropertyInfo"/> instance has been locked for further modification. |
| | | 41 | | /// </exception> |
| | | 42 | | /// <remarks> |
| | | 43 | | /// It is possible to use <see cref="JsonConverterFactory"/> instances with this property. |
| | | 44 | | /// |
| | | 45 | | /// For contracts originating from <see cref="DefaultJsonTypeInfoResolver"/>, the value of |
| | | 46 | | /// <see cref="CustomConverter"/> will be mapped from <see cref="JsonConverterAttribute" /> annotations. |
| | | 47 | | /// </remarks> |
| | | 48 | | public JsonConverter? CustomConverter |
| | | 49 | | { |
| | 20048 | 50 | | get => _customConverter; |
| | | 51 | | set |
| | 7794 | 52 | | { |
| | 7794 | 53 | | VerifyMutable(); |
| | 7794 | 54 | | _customConverter = value; |
| | 7794 | 55 | | } |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | private JsonConverter? _customConverter; |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Gets or sets a getter delegate for the property. |
| | | 62 | | /// </summary> |
| | | 63 | | /// <exception cref="InvalidOperationException"> |
| | | 64 | | /// The <see cref="JsonPropertyInfo"/> instance has been locked for further modification. |
| | | 65 | | /// </exception> |
| | | 66 | | /// <remarks> |
| | | 67 | | /// Setting to <see langword="null"/> will result in the property being skipped on serialization. |
| | | 68 | | /// </remarks> |
| | | 69 | | public Func<object, object?>? Get |
| | | 70 | | { |
| | 7794 | 71 | | get => _untypedGet; |
| | | 72 | | set |
| | 0 | 73 | | { |
| | 0 | 74 | | VerifyMutable(); |
| | 0 | 75 | | SetGetter(value); |
| | 0 | 76 | | } |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Gets or sets a setter delegate for the property. |
| | | 81 | | /// </summary> |
| | | 82 | | /// <exception cref="InvalidOperationException"> |
| | | 83 | | /// The <see cref="JsonPropertyInfo"/> instance has been locked for further modification. |
| | | 84 | | /// </exception> |
| | | 85 | | /// <remarks> |
| | | 86 | | /// Setting to <see langword="null"/> will result in the property being skipped on deserialization. |
| | | 87 | | /// </remarks> |
| | | 88 | | public Action<object, object?>? Set |
| | | 89 | | { |
| | 6957 | 90 | | get => _untypedSet; |
| | | 91 | | set |
| | 0 | 92 | | { |
| | 0 | 93 | | VerifyMutable(); |
| | 0 | 94 | | SetSetter(value); |
| | 0 | 95 | | _isUserSpecifiedSetter = true; |
| | 0 | 96 | | } |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | private protected Func<object, object?>? _untypedGet; |
| | | 100 | | private protected Action<object, object?>? _untypedSet; |
| | | 101 | | private bool _isUserSpecifiedSetter; |
| | | 102 | | |
| | | 103 | | private protected abstract void SetGetter(Delegate? getter); |
| | | 104 | | private protected abstract void SetSetter(Delegate? setter); |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// Gets or sets a predicate deciding whether the current property value should be serialized. |
| | | 108 | | /// </summary> |
| | | 109 | | /// <exception cref="InvalidOperationException"> |
| | | 110 | | /// The <see cref="JsonPropertyInfo"/> instance has been locked for further modification. |
| | | 111 | | /// </exception> |
| | | 112 | | /// <remarks> |
| | | 113 | | /// The first parameter denotes the parent object, the second parameter denotes the property value. |
| | | 114 | | /// |
| | | 115 | | /// Setting the predicate to <see langword="null"/> is equivalent to always serializing the property value. |
| | | 116 | | /// |
| | | 117 | | /// For contracts originating from <see cref="DefaultJsonTypeInfoResolver"/>, |
| | | 118 | | /// the value of <see cref="JsonIgnoreAttribute.Condition"/> will map to this predicate. |
| | | 119 | | /// </remarks> |
| | | 120 | | public Func<object, object?, bool>? ShouldSerialize |
| | | 121 | | { |
| | 0 | 122 | | get => _shouldSerialize; |
| | | 123 | | set |
| | 0 | 124 | | { |
| | 0 | 125 | | VerifyMutable(); |
| | 0 | 126 | | SetShouldSerialize(value); |
| | | 127 | | // Invalidate any JsonIgnore configuration if delegate set manually by user |
| | 0 | 128 | | _isUserSpecifiedShouldSerialize = true; |
| | 0 | 129 | | IgnoreDefaultValuesOnWrite = false; |
| | 0 | 130 | | } |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | private protected Func<object, object?, bool>? _shouldSerialize; |
| | | 134 | | private bool _isUserSpecifiedShouldSerialize; |
| | | 135 | | private protected abstract void SetShouldSerialize(Delegate? predicate); |
| | | 136 | | |
| | | 137 | | internal JsonIgnoreCondition? IgnoreCondition |
| | | 138 | | { |
| | 0 | 139 | | get => _ignoreCondition; |
| | | 140 | | set |
| | 7794 | 141 | | { |
| | 7794 | 142 | | Debug.Assert(!IsConfigured); |
| | 7794 | 143 | | ConfigureIgnoreCondition(value); |
| | 7794 | 144 | | _ignoreCondition = value; |
| | 7794 | 145 | | } |
| | | 146 | | } |
| | | 147 | | |
| | | 148 | | private JsonIgnoreCondition? _ignoreCondition; |
| | | 149 | | private protected abstract void ConfigureIgnoreCondition(JsonIgnoreCondition? ignoreCondition); |
| | | 150 | | |
| | | 151 | | /// <summary> |
| | | 152 | | /// Gets or sets a custom attribute provider for the current property. |
| | | 153 | | /// </summary> |
| | | 154 | | /// <exception cref="InvalidOperationException"> |
| | | 155 | | /// The <see cref="JsonPropertyInfo"/> instance has been locked for further modification. |
| | | 156 | | /// </exception> |
| | | 157 | | /// <remarks> |
| | | 158 | | /// When resolving metadata via the built-in resolvers this |
| | | 159 | | /// will be populated with the underlying <see cref="MemberInfo" /> of the serialized property or field. |
| | | 160 | | /// |
| | | 161 | | /// Setting a custom attribute provider will have no impact on the contract model, |
| | | 162 | | /// but serves as metadata for downstream contract modifiers. |
| | | 163 | | /// </remarks> |
| | | 164 | | public ICustomAttributeProvider? AttributeProvider |
| | | 165 | | { |
| | | 166 | | get |
| | 7794 | 167 | | { |
| | 7794 | 168 | | Func<ICustomAttributeProvider>? attributeProviderFactory = Volatile.Read(ref AttributeProviderFactory); |
| | 7794 | 169 | | ICustomAttributeProvider? attributeProvider = _attributeProvider; |
| | | 170 | | |
| | 7794 | 171 | | if (attributeProvider is null && attributeProviderFactory is not null) |
| | 0 | 172 | | { |
| | 0 | 173 | | _attributeProvider = attributeProvider = attributeProviderFactory(); |
| | 0 | 174 | | Volatile.Write(ref AttributeProviderFactory, null); |
| | 0 | 175 | | } |
| | | 176 | | |
| | 7794 | 177 | | return attributeProvider; |
| | 7794 | 178 | | } |
| | | 179 | | set |
| | 7794 | 180 | | { |
| | 7794 | 181 | | VerifyMutable(); |
| | | 182 | | |
| | 7794 | 183 | | _attributeProvider = value; |
| | 7794 | 184 | | Volatile.Write(ref AttributeProviderFactory, null); |
| | 7794 | 185 | | } |
| | | 186 | | } |
| | | 187 | | |
| | | 188 | | // Metadata emanating from the source generator use delayed attribute provider initialization |
| | | 189 | | // ensuring that reflection metadata resolution remains pay-for-play and is trimmable. |
| | | 190 | | internal Func<ICustomAttributeProvider>? AttributeProviderFactory; |
| | | 191 | | private ICustomAttributeProvider? _attributeProvider; |
| | | 192 | | |
| | | 193 | | /// <summary> |
| | | 194 | | /// Gets or sets a value indicating if the property or field should be replaced or populated during deserializat |
| | | 195 | | /// </summary> |
| | | 196 | | /// <remarks> |
| | | 197 | | /// Initial value for this property is based on the presence of <see cref="JsonObjectCreationHandlingAttribute"/ |
| | | 198 | | /// When <see langword="null"/> effective handling will be resolved based on |
| | | 199 | | /// capability of property converter to populate, containing type's <see cref="JsonTypeInfo.PreferredPropertyObj |
| | | 200 | | /// and <see cref="JsonSerializerOptions.PreferredObjectCreationHandling"/> value. |
| | | 201 | | /// </remarks> |
| | | 202 | | public JsonObjectCreationHandling? ObjectCreationHandling |
| | | 203 | | { |
| | 20048 | 204 | | get => _objectCreationHandling; |
| | | 205 | | set |
| | 7794 | 206 | | { |
| | 7794 | 207 | | VerifyMutable(); |
| | | 208 | | |
| | 7794 | 209 | | if (value != null) |
| | 0 | 210 | | { |
| | 0 | 211 | | if (!JsonSerializer.IsValidCreationHandlingValue(value.Value)) |
| | 0 | 212 | | { |
| | 0 | 213 | | throw new ArgumentOutOfRangeException(nameof(value)); |
| | | 214 | | } |
| | 0 | 215 | | } |
| | | 216 | | |
| | 7794 | 217 | | _objectCreationHandling = value; |
| | 7794 | 218 | | } |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | private JsonObjectCreationHandling? _objectCreationHandling; |
| | 32302 | 222 | | internal JsonObjectCreationHandling EffectiveObjectCreationHandling { get; private set; } |
| | | 223 | | |
| | 32370 | 224 | | internal string? MemberName { get; set; } // Do not rename (legacy schema generation) |
| | 50550 | 225 | | internal MemberTypes MemberType { get; set; } |
| | 7794 | 226 | | internal bool IsVirtual { get; set; } |
| | | 227 | | |
| | | 228 | | /// <summary> |
| | | 229 | | /// Gets or sets a value indicating whether the return type of the getter is annotated as nullable. |
| | | 230 | | /// </summary> |
| | | 231 | | /// <exception cref="InvalidOperationException"> |
| | | 232 | | /// The <see cref="JsonPropertyInfo"/> instance has been locked for further modification. |
| | | 233 | | /// |
| | | 234 | | /// -or- |
| | | 235 | | /// |
| | | 236 | | /// The current <see cref="PropertyType"/> is not a reference type or <see cref="Nullable{T}"/>. |
| | | 237 | | /// </exception> |
| | | 238 | | /// <remarks> |
| | | 239 | | /// Contracts originating from <see cref="DefaultJsonTypeInfoResolver"/> or <see cref="JsonSerializerContext"/>, |
| | | 240 | | /// derive the value of this property from nullable reference type annotations, including annotations |
| | | 241 | | /// from attributes such as <see cref="NotNullAttribute"/> or <see cref="MaybeNullAttribute"/>. |
| | | 242 | | /// |
| | | 243 | | /// This property has no effect on serialization unless the <see cref="JsonSerializerOptions.RespectNullableAnno |
| | | 244 | | /// property has been enabled, in which case the serializer will reject any <see langword="null"/> values return |
| | | 245 | | /// </remarks> |
| | | 246 | | public bool IsGetNullable |
| | | 247 | | { |
| | 0 | 248 | | get => _isGetNullable; |
| | | 249 | | set |
| | 2660 | 250 | | { |
| | 2660 | 251 | | VerifyMutable(); |
| | | 252 | | |
| | 2660 | 253 | | if (value && !PropertyTypeCanBeNull) |
| | 0 | 254 | | { |
| | 0 | 255 | | ThrowHelper.ThrowInvalidOperationException_PropertyTypeNotNullable(this); |
| | | 256 | | } |
| | | 257 | | |
| | 2660 | 258 | | _isGetNullable = value; |
| | 2660 | 259 | | } |
| | | 260 | | } |
| | | 261 | | |
| | | 262 | | private bool _isGetNullable; |
| | | 263 | | |
| | | 264 | | /// <summary> |
| | | 265 | | /// Gets or sets a value indicating whether the input type of the setter is annotated as nullable. |
| | | 266 | | /// </summary> |
| | | 267 | | /// <exception cref="InvalidOperationException"> |
| | | 268 | | /// The <see cref="JsonPropertyInfo"/> instance has been locked for further modification. |
| | | 269 | | /// |
| | | 270 | | /// -or- |
| | | 271 | | /// |
| | | 272 | | /// The current <see cref="PropertyType"/> is not a reference type or <see cref="Nullable{T}"/>. |
| | | 273 | | /// </exception> |
| | | 274 | | /// <remarks> |
| | | 275 | | /// Contracts originating from <see cref="DefaultJsonTypeInfoResolver"/> or <see cref="JsonSerializerContext"/>, |
| | | 276 | | /// derive the value of this property from nullable reference type annotations, including annotations |
| | | 277 | | /// from attributes such as <see cref="AllowNullAttribute"/> or <see cref="DisallowNullAttribute"/>. |
| | | 278 | | /// |
| | | 279 | | /// This property has no effect on deserialization unless the <see cref="JsonSerializerOptions.RespectNullableAn |
| | | 280 | | /// property has been enabled, in which case the serializer will reject any <see langword="null"/> deserializati |
| | | 281 | | /// |
| | | 282 | | /// If the property has been associated with a deserialization constructor parameter, |
| | | 283 | | /// this setting reflected the nullability annotation of the parameter and not the property setter. |
| | | 284 | | /// </remarks> |
| | | 285 | | public bool IsSetNullable |
| | | 286 | | { |
| | 0 | 287 | | get => _isSetNullable; |
| | | 288 | | set |
| | 2660 | 289 | | { |
| | 2660 | 290 | | VerifyMutable(); |
| | | 291 | | |
| | 2660 | 292 | | if (value && !PropertyTypeCanBeNull) |
| | 0 | 293 | | { |
| | 0 | 294 | | ThrowHelper.ThrowInvalidOperationException_PropertyTypeNotNullable(this); |
| | | 295 | | } |
| | | 296 | | |
| | 2660 | 297 | | _isSetNullable = value; |
| | 2660 | 298 | | } |
| | | 299 | | } |
| | | 300 | | |
| | | 301 | | private protected bool _isSetNullable; |
| | | 302 | | |
| | | 303 | | /// <summary> |
| | | 304 | | /// Specifies whether the current property is a special extension data property. |
| | | 305 | | /// </summary> |
| | | 306 | | /// <exception cref="InvalidOperationException"> |
| | | 307 | | /// The <see cref="JsonPropertyInfo"/> instance has been locked for further modification. |
| | | 308 | | /// |
| | | 309 | | /// -or- |
| | | 310 | | /// |
| | | 311 | | /// The current <see cref="PropertyType"/> is not valid for use with extension data. |
| | | 312 | | /// </exception> |
| | | 313 | | /// <remarks> |
| | | 314 | | /// For contracts originating from <see cref="DefaultJsonTypeInfoResolver"/> or <see cref="JsonSerializerContext |
| | | 315 | | /// the value of this property will be mapped from <see cref="JsonExtensionDataAttribute"/> annotations. |
| | | 316 | | /// </remarks> |
| | | 317 | | public bool IsExtensionData |
| | | 318 | | { |
| | 7794 | 319 | | get => _isExtensionDataProperty; |
| | | 320 | | set |
| | 7794 | 321 | | { |
| | 7794 | 322 | | VerifyMutable(); |
| | | 323 | | |
| | 7794 | 324 | | if (value && !JsonTypeInfo.IsValidExtensionDataProperty(PropertyType)) |
| | 0 | 325 | | { |
| | 0 | 326 | | ThrowHelper.ThrowInvalidOperationException_SerializationDataExtensionPropertyInvalid(this); |
| | | 327 | | } |
| | | 328 | | |
| | 7794 | 329 | | _isExtensionDataProperty = value; |
| | 7794 | 330 | | } |
| | | 331 | | } |
| | | 332 | | |
| | | 333 | | private bool _isExtensionDataProperty; |
| | | 334 | | |
| | | 335 | | /// <summary> |
| | | 336 | | /// Specifies whether the current property is required for deserialization to be successful. |
| | | 337 | | /// </summary> |
| | | 338 | | /// <exception cref="InvalidOperationException"> |
| | | 339 | | /// The <see cref="JsonPropertyInfo"/> instance has been locked for further modification. |
| | | 340 | | /// </exception> |
| | | 341 | | /// <remarks> |
| | | 342 | | /// For contracts originating from <see cref="DefaultJsonTypeInfoResolver"/> or <see cref="JsonSerializerContext |
| | | 343 | | /// the value of this property will be mapped from <see cref="JsonRequiredAttribute"/> annotations. |
| | | 344 | | /// |
| | | 345 | | /// For contracts using <see cref="DefaultJsonTypeInfoResolver"/>, properties using the <see langword="required" |
| | | 346 | | /// will also map to this setting, unless deserialization uses a SetsRequiredMembersAttribute on a constructor t |
| | | 347 | | /// <see langword="required"/> keyword is currently not supported in <see cref="JsonSerializerContext"/> contrac |
| | | 348 | | /// </remarks> |
| | | 349 | | public bool IsRequired |
| | | 350 | | { |
| | 27842 | 351 | | get => _isRequired; |
| | | 352 | | set |
| | 7794 | 353 | | { |
| | 7794 | 354 | | VerifyMutable(); |
| | 7794 | 355 | | _isRequired = value; |
| | 7794 | 356 | | } |
| | | 357 | | } |
| | | 358 | | |
| | | 359 | | private protected bool _isRequired; |
| | | 360 | | |
| | | 361 | | /// <summary> |
| | | 362 | | /// Gets the constructor parameter associated with the current property. |
| | | 363 | | /// </summary> |
| | | 364 | | /// <remarks> |
| | | 365 | | /// Returns the <see cref="JsonParameterInfo"/> metadata for the parameter in the |
| | | 366 | | /// deserialization constructor that has been associated with the current property. |
| | | 367 | | /// |
| | | 368 | | /// A constructor parameter is matched to a property or field if they are of the |
| | | 369 | | /// same type and have the same name, up to case insensitivity. Each constructor |
| | | 370 | | /// parameter must be matched to exactly one property of field. |
| | | 371 | | /// </remarks> |
| | 13482 | 372 | | public JsonParameterInfo? AssociatedParameter { get; internal set; } |
| | | 373 | | |
| | 20048 | 374 | | internal JsonPropertyInfo(Type declaringType, Type propertyType, JsonTypeInfo? declaringTypeInfo, JsonSerializer |
| | 20048 | 375 | | { |
| | 20048 | 376 | | Debug.Assert(declaringTypeInfo is null || declaringType.IsAssignableFrom(declaringTypeInfo.Type)); |
| | | 377 | | |
| | 20048 | 378 | | DeclaringType = declaringType; |
| | 20048 | 379 | | PropertyType = propertyType; |
| | 20048 | 380 | | DeclaringTypeInfo = declaringTypeInfo; // null declaringTypeInfo means it's not tied yet |
| | 20048 | 381 | | Options = options; |
| | | 382 | | |
| | 20048 | 383 | | _isGetNullable = _isSetNullable = PropertyTypeCanBeNull; |
| | 20048 | 384 | | } |
| | | 385 | | |
| | | 386 | | internal static JsonPropertyInfo GetPropertyPlaceholder() |
| | 0 | 387 | | { |
| | 0 | 388 | | JsonPropertyInfo info = new JsonPropertyInfo<object>(typeof(object), declaringTypeInfo: null, options: null! |
| | | 389 | | |
| | 0 | 390 | | Debug.Assert(!info.IsForTypeInfo); |
| | 0 | 391 | | Debug.Assert(!info.CanSerialize); |
| | 0 | 392 | | Debug.Assert(!info.CanDeserialize); |
| | | 393 | | |
| | 0 | 394 | | info.Name = string.Empty; |
| | | 395 | | |
| | 0 | 396 | | return info; |
| | 0 | 397 | | } |
| | | 398 | | |
| | | 399 | | /// <summary> |
| | | 400 | | /// Gets the declaring type of the property. |
| | | 401 | | /// </summary> |
| | 0 | 402 | | public Type DeclaringType { get; } |
| | | 403 | | |
| | | 404 | | /// <summary> |
| | | 405 | | /// Gets the type of the current property. |
| | | 406 | | /// </summary> |
| | 81794 | 407 | | public Type PropertyType { get; } |
| | | 408 | | |
| | | 409 | | private protected void VerifyMutable() |
| | 67672 | 410 | | { |
| | 67672 | 411 | | DeclaringTypeInfo?.VerifyMutable(); |
| | 67672 | 412 | | } |
| | | 413 | | |
| | 128150 | 414 | | internal bool IsConfigured { get; private set; } |
| | | 415 | | |
| | | 416 | | internal void Configure() |
| | 20048 | 417 | | { |
| | 20048 | 418 | | Debug.Assert(DeclaringTypeInfo != null); |
| | 20048 | 419 | | Debug.Assert(!IsConfigured); |
| | | 420 | | |
| | 20048 | 421 | | if (IsIgnored) |
| | 0 | 422 | | { |
| | | 423 | | // Avoid configuring JsonIgnore.Always properties |
| | | 424 | | // to avoid failing on potentially unsupported types. |
| | 0 | 425 | | CanSerialize = false; |
| | 0 | 426 | | CanDeserialize = false; |
| | 0 | 427 | | } |
| | | 428 | | else |
| | 20048 | 429 | | { |
| | 20048 | 430 | | _jsonTypeInfo ??= Options.GetTypeInfoInternal(PropertyType); |
| | 20048 | 431 | | _jsonTypeInfo.EnsureConfigured(); |
| | | 432 | | |
| | 20048 | 433 | | DetermineEffectiveConverter(_jsonTypeInfo); |
| | 20048 | 434 | | DetermineNumberHandlingForProperty(); |
| | 20048 | 435 | | DetermineEffectiveObjectCreationHandlingForProperty(); |
| | 20048 | 436 | | DetermineSerializationCapabilities(); |
| | 20048 | 437 | | DetermineIgnoreCondition(); |
| | 20048 | 438 | | } |
| | | 439 | | |
| | 20048 | 440 | | if (IsForTypeInfo) |
| | 12254 | 441 | | { |
| | 12254 | 442 | | DetermineNumberHandlingForTypeInfo(); |
| | 12254 | 443 | | } |
| | | 444 | | else |
| | 7794 | 445 | | { |
| | 7794 | 446 | | ValidateAndCachePropertyName(); |
| | 7794 | 447 | | } |
| | | 448 | | |
| | 20048 | 449 | | if (IsRequired) |
| | 0 | 450 | | { |
| | 0 | 451 | | if (!CanDeserialize && |
| | 0 | 452 | | !(AssociatedParameter?.IsRequiredParameter is true && |
| | 0 | 453 | | Options.RespectRequiredConstructorParameters)) |
| | 0 | 454 | | { |
| | 0 | 455 | | ThrowHelper.ThrowInvalidOperationException_JsonPropertyRequiredAndNotDeserializable(this); |
| | | 456 | | } |
| | | 457 | | |
| | 0 | 458 | | if (IsExtensionData) |
| | 0 | 459 | | { |
| | 0 | 460 | | ThrowHelper.ThrowInvalidOperationException_JsonPropertyRequiredAndExtensionData(this); |
| | | 461 | | } |
| | | 462 | | |
| | 0 | 463 | | Debug.Assert(!IgnoreNullTokensOnRead); |
| | 0 | 464 | | } |
| | | 465 | | |
| | 20048 | 466 | | IsConfigured = true; |
| | 20048 | 467 | | } |
| | | 468 | | |
| | | 469 | | private protected abstract void DetermineEffectiveConverter(JsonTypeInfo jsonTypeInfo); |
| | | 470 | | |
| | | 471 | | [RequiresUnreferencedCode(JsonSerializer.SerializationUnreferencedCodeMessage)] |
| | | 472 | | [RequiresDynamicCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)] |
| | | 473 | | internal abstract void DetermineReflectionPropertyAccessors(MemberInfo memberInfo, bool useNonPublicAccessors); |
| | | 474 | | |
| | | 475 | | private void ValidateAndCachePropertyName() |
| | 7794 | 476 | | { |
| | 7794 | 477 | | Debug.Assert(Name != null); |
| | | 478 | | |
| | 7794 | 479 | | if (Options.ReferenceHandlingStrategy is JsonKnownReferenceHandler.Preserve && |
| | 7794 | 480 | | this is { DeclaringType.IsValueType: false, IsIgnored: false, IsExtensionData: false } && |
| | 7794 | 481 | | Name is JsonSerializer.IdPropertyName or JsonSerializer.RefPropertyName) |
| | 0 | 482 | | { |
| | | 483 | | // Validate potential conflicts with reference preservation metadata property names. |
| | | 484 | | // Conflicts with polymorphic type discriminators are contextual and need to be |
| | | 485 | | // handled separately by the PolymorphicTypeResolver type. |
| | | 486 | | |
| | 0 | 487 | | ThrowHelper.ThrowInvalidOperationException_PropertyConflictsWithMetadataPropertyName(DeclaringType, Name |
| | | 488 | | } |
| | | 489 | | |
| | 7794 | 490 | | NameAsUtf8Bytes = Encoding.UTF8.GetBytes(Name); |
| | 7794 | 491 | | EscapedNameSection = JsonHelpers.GetEscapedPropertyNameSection(NameAsUtf8Bytes, Options.Encoder); |
| | 7794 | 492 | | } |
| | | 493 | | |
| | | 494 | | private void DetermineIgnoreCondition() |
| | 20048 | 495 | | { |
| | 20048 | 496 | | if (_ignoreCondition != null) |
| | 0 | 497 | | { |
| | | 498 | | // Do not apply global policy if already configured on the property level. |
| | 0 | 499 | | return; |
| | | 500 | | } |
| | | 501 | | |
| | | 502 | | #pragma warning disable SYSLIB0020 // JsonSerializerOptions.IgnoreNullValues is obsolete |
| | 20048 | 503 | | if (Options.IgnoreNullValues) |
| | | 504 | | #pragma warning restore SYSLIB0020 |
| | 0 | 505 | | { |
| | 0 | 506 | | Debug.Assert(Options.DefaultIgnoreCondition == JsonIgnoreCondition.Never); |
| | 0 | 507 | | if (PropertyTypeCanBeNull) |
| | 0 | 508 | | { |
| | 0 | 509 | | IgnoreNullTokensOnRead = !_isUserSpecifiedSetter && !IsRequired; |
| | 0 | 510 | | IgnoreDefaultValuesOnWrite = ShouldSerialize is null; |
| | 0 | 511 | | } |
| | 0 | 512 | | } |
| | 20048 | 513 | | else if (Options.DefaultIgnoreCondition == JsonIgnoreCondition.WhenWritingNull) |
| | 0 | 514 | | { |
| | 0 | 515 | | if (PropertyTypeCanBeNull) |
| | 0 | 516 | | { |
| | 0 | 517 | | IgnoreDefaultValuesOnWrite = ShouldSerialize is null; |
| | 0 | 518 | | } |
| | 0 | 519 | | } |
| | 20048 | 520 | | else if (Options.DefaultIgnoreCondition == JsonIgnoreCondition.WhenWritingDefault) |
| | 0 | 521 | | { |
| | 0 | 522 | | IgnoreDefaultValuesOnWrite = ShouldSerialize is null; |
| | 0 | 523 | | } |
| | 20048 | 524 | | } |
| | | 525 | | |
| | | 526 | | private void DetermineSerializationCapabilities() |
| | 20048 | 527 | | { |
| | 20048 | 528 | | Debug.Assert(EffectiveConverter != null, "Must have calculated the effective converter."); |
| | 20048 | 529 | | CanSerialize = HasGetter; |
| | 20048 | 530 | | CanDeserialize = HasSetter; |
| | | 531 | | |
| | 20048 | 532 | | Debug.Assert(MemberType is 0 or MemberTypes.Field or MemberTypes.Property); |
| | 20048 | 533 | | if (MemberType == 0 || _ignoreCondition != null) |
| | 12254 | 534 | | { |
| | | 535 | | // No policy to be applied if either: |
| | | 536 | | // 1. JsonPropertyInfo is a custom instance (not generated via reflection or sourcegen). |
| | | 537 | | // 2. A JsonIgnoreCondition has been specified on the property level. |
| | 12254 | 538 | | CanDeserializeOrPopulate = CanDeserialize || EffectiveObjectCreationHandling == JsonObjectCreationHandli |
| | 12254 | 539 | | return; |
| | | 540 | | } |
| | | 541 | | |
| | 7794 | 542 | | if ((EffectiveConverter.ConverterStrategy & (ConverterStrategy.Enumerable | ConverterStrategy.Dictionary)) ! |
| | 837 | 543 | | { |
| | | 544 | | // Properties of collections types that only have setters are not supported. |
| | 837 | 545 | | if (Get == null && Set != null && !_isUserSpecifiedSetter) |
| | 0 | 546 | | { |
| | 0 | 547 | | CanDeserialize = false; |
| | 0 | 548 | | } |
| | 837 | 549 | | } |
| | | 550 | | else |
| | 6957 | 551 | | { |
| | | 552 | | // For read-only properties of non-collection types, apply IgnoreReadOnlyProperties/Fields policy, |
| | | 553 | | // unless a `ShouldSerialize` predicate has been explicitly applied by the user (null or non-null). |
| | 6957 | 554 | | if (Get != null && Set == null && IgnoreReadOnlyMember && !_isUserSpecifiedShouldSerialize) |
| | 0 | 555 | | { |
| | 0 | 556 | | CanSerialize = false; |
| | 0 | 557 | | } |
| | 6957 | 558 | | } |
| | | 559 | | |
| | 7794 | 560 | | CanDeserializeOrPopulate = CanDeserialize || EffectiveObjectCreationHandling == JsonObjectCreationHandling.P |
| | 20048 | 561 | | } |
| | | 562 | | |
| | | 563 | | private void DetermineNumberHandlingForTypeInfo() |
| | 12254 | 564 | | { |
| | 12254 | 565 | | Debug.Assert(DeclaringTypeInfo != null, "We should have ensured parent is assigned in JsonTypeInfo"); |
| | 12254 | 566 | | Debug.Assert(!DeclaringTypeInfo.IsConfigured); |
| | | 567 | | |
| | 12254 | 568 | | JsonNumberHandling? declaringTypeNumberHandling = DeclaringTypeInfo.NumberHandling; |
| | | 569 | | |
| | 12254 | 570 | | if (declaringTypeNumberHandling != null && declaringTypeNumberHandling != JsonNumberHandling.Strict && !Effe |
| | 0 | 571 | | { |
| | 0 | 572 | | ThrowHelper.ThrowInvalidOperationException_NumberHandlingOnPropertyInvalid(this); |
| | | 573 | | } |
| | | 574 | | |
| | 12254 | 575 | | if (NumberHandingIsApplicable()) |
| | 4691 | 576 | | { |
| | | 577 | | // This logic is to honor JsonNumberHandlingAttribute placed on |
| | | 578 | | // custom collections e.g. public class MyNumberList : List<int>. |
| | | 579 | | |
| | | 580 | | // Priority 1: Get handling from the type (parent type in this case is the type itself). |
| | 4691 | 581 | | EffectiveNumberHandling = declaringTypeNumberHandling; |
| | | 582 | | |
| | | 583 | | // Priority 2: Get handling from JsonSerializerOptions instance. |
| | 4691 | 584 | | if (!EffectiveNumberHandling.HasValue && Options.NumberHandling != JsonNumberHandling.Strict) |
| | 3450 | 585 | | { |
| | 3450 | 586 | | EffectiveNumberHandling = Options.NumberHandling; |
| | 3450 | 587 | | } |
| | 4691 | 588 | | } |
| | 12254 | 589 | | } |
| | | 590 | | |
| | | 591 | | private void DetermineNumberHandlingForProperty() |
| | 20048 | 592 | | { |
| | 20048 | 593 | | Debug.Assert(DeclaringTypeInfo != null, "We should have ensured parent is assigned in JsonTypeInfo"); |
| | 20048 | 594 | | Debug.Assert(!IsConfigured, "Should not be called post-configuration."); |
| | 20048 | 595 | | Debug.Assert(_jsonTypeInfo != null, "Must have already been determined on configuration."); |
| | | 596 | | |
| | 20048 | 597 | | bool numberHandlingIsApplicable = NumberHandingIsApplicable(); |
| | | 598 | | |
| | 20048 | 599 | | if (numberHandlingIsApplicable) |
| | 6387 | 600 | | { |
| | | 601 | | // Priority 1: Get handling from attribute on property/field, its parent class type or property type. |
| | 6387 | 602 | | JsonNumberHandling? handling = NumberHandling ?? DeclaringTypeInfo.NumberHandling ?? _jsonTypeInfo.Numbe |
| | | 603 | | |
| | | 604 | | // Priority 2: Get handling from JsonSerializerOptions instance. |
| | 6387 | 605 | | if (!handling.HasValue && Options.NumberHandling != JsonNumberHandling.Strict) |
| | 4637 | 606 | | { |
| | 4637 | 607 | | handling = Options.NumberHandling; |
| | 4637 | 608 | | } |
| | | 609 | | |
| | 6387 | 610 | | EffectiveNumberHandling = handling; |
| | 6387 | 611 | | } |
| | 13661 | 612 | | else if (NumberHandling.HasValue && NumberHandling != JsonNumberHandling.Strict) |
| | 0 | 613 | | { |
| | 0 | 614 | | ThrowHelper.ThrowInvalidOperationException_NumberHandlingOnPropertyInvalid(this); |
| | | 615 | | } |
| | 20048 | 616 | | } |
| | | 617 | | |
| | | 618 | | private void DetermineEffectiveObjectCreationHandlingForProperty() |
| | 20048 | 619 | | { |
| | 20048 | 620 | | Debug.Assert(EffectiveConverter != null, "Must have calculated the effective converter."); |
| | 20048 | 621 | | Debug.Assert(DeclaringTypeInfo != null, "We should have ensured parent is assigned in JsonTypeInfo"); |
| | 20048 | 622 | | Debug.Assert(!IsConfigured, "Should not be called post-configuration."); |
| | | 623 | | |
| | 20048 | 624 | | JsonObjectCreationHandling effectiveObjectCreationHandling = JsonObjectCreationHandling.Replace; |
| | 20048 | 625 | | if (ObjectCreationHandling == null) |
| | 20048 | 626 | | { |
| | | 627 | | // Consult type-level configuration, then global configuration. |
| | | 628 | | // Ignore global configuration if we're using a parameterized constructor. |
| | 20048 | 629 | | JsonObjectCreationHandling preferredCreationHandling = |
| | 20048 | 630 | | DeclaringTypeInfo.PreferredPropertyObjectCreationHandling |
| | 20048 | 631 | | ?? (DeclaringTypeInfo.DetermineUsesParameterizedConstructor() |
| | 20048 | 632 | | ? JsonObjectCreationHandling.Replace |
| | 20048 | 633 | | : Options.PreferredObjectCreationHandling); |
| | | 634 | | |
| | 20048 | 635 | | bool canPopulate = |
| | 20048 | 636 | | preferredCreationHandling == JsonObjectCreationHandling.Populate && |
| | 20048 | 637 | | EffectiveConverter.CanPopulate && |
| | 20048 | 638 | | Get != null && |
| | 20048 | 639 | | (!PropertyType.IsValueType || Set != null) && |
| | 20048 | 640 | | !DeclaringTypeInfo.SupportsPolymorphicDeserialization && |
| | 20048 | 641 | | !(Set == null && IgnoreReadOnlyMember); |
| | | 642 | | |
| | 20048 | 643 | | effectiveObjectCreationHandling = canPopulate ? JsonObjectCreationHandling.Populate : JsonObjectCreation |
| | 20048 | 644 | | } |
| | 0 | 645 | | else if (ObjectCreationHandling == JsonObjectCreationHandling.Populate) |
| | 0 | 646 | | { |
| | 0 | 647 | | if (!EffectiveConverter.CanPopulate) |
| | 0 | 648 | | { |
| | 0 | 649 | | ThrowHelper.ThrowInvalidOperationException_ObjectCreationHandlingPopulateNotSupportedByConverter(thi |
| | | 650 | | } |
| | | 651 | | |
| | 0 | 652 | | if (Get == null) |
| | 0 | 653 | | { |
| | 0 | 654 | | ThrowHelper.ThrowInvalidOperationException_ObjectCreationHandlingPropertyMustHaveAGetter(this); |
| | | 655 | | } |
| | | 656 | | |
| | 0 | 657 | | if (PropertyType.IsValueType && Set == null) |
| | 0 | 658 | | { |
| | 0 | 659 | | ThrowHelper.ThrowInvalidOperationException_ObjectCreationHandlingPropertyValueTypeMustHaveASetter(th |
| | | 660 | | } |
| | | 661 | | |
| | 0 | 662 | | Debug.Assert(_jsonTypeInfo != null); |
| | 0 | 663 | | Debug.Assert(_jsonTypeInfo.IsConfigurationStarted); |
| | 0 | 664 | | if (JsonTypeInfo.SupportsPolymorphicDeserialization) |
| | 0 | 665 | | { |
| | 0 | 666 | | ThrowHelper.ThrowInvalidOperationException_ObjectCreationHandlingPropertyCannotAllowPolymorphicDeser |
| | | 667 | | } |
| | | 668 | | |
| | 0 | 669 | | if (Set == null && IgnoreReadOnlyMember) |
| | 0 | 670 | | { |
| | 0 | 671 | | ThrowHelper.ThrowInvalidOperationException_ObjectCreationHandlingPropertyCannotAllowReadOnlyMember(t |
| | | 672 | | } |
| | | 673 | | |
| | 0 | 674 | | effectiveObjectCreationHandling = JsonObjectCreationHandling.Populate; |
| | 0 | 675 | | } |
| | | 676 | | |
| | 20048 | 677 | | if (effectiveObjectCreationHandling is JsonObjectCreationHandling.Populate) |
| | 0 | 678 | | { |
| | 0 | 679 | | if (DeclaringTypeInfo.DetermineUsesParameterizedConstructor()) |
| | 0 | 680 | | { |
| | 0 | 681 | | ThrowHelper.ThrowNotSupportedException_ObjectCreationHandlingPropertyDoesNotSupportParameterizedCons |
| | | 682 | | } |
| | | 683 | | |
| | 0 | 684 | | if (Options.ReferenceHandlingStrategy != JsonKnownReferenceHandler.Unspecified) |
| | 0 | 685 | | { |
| | 0 | 686 | | ThrowHelper.ThrowInvalidOperationException_ObjectCreationHandlingPropertyCannotAllowReferenceHandlin |
| | | 687 | | } |
| | 0 | 688 | | } |
| | | 689 | | |
| | | 690 | | // Validation complete, commit configuration. |
| | 20048 | 691 | | EffectiveObjectCreationHandling = effectiveObjectCreationHandling; |
| | 20048 | 692 | | } |
| | | 693 | | |
| | | 694 | | private bool NumberHandingIsApplicable() |
| | 32302 | 695 | | { |
| | 32302 | 696 | | if (EffectiveConverter.IsInternalConverterForNumberType) |
| | 9002 | 697 | | { |
| | 9002 | 698 | | return true; |
| | | 699 | | } |
| | | 700 | | |
| | | 701 | | Type potentialNumberType; |
| | 23300 | 702 | | if (!EffectiveConverter.IsInternalConverter || |
| | 23300 | 703 | | ((ConverterStrategy.Enumerable | ConverterStrategy.Dictionary) & EffectiveConverter.ConverterStrategy) = |
| | 20459 | 704 | | { |
| | 20459 | 705 | | potentialNumberType = PropertyType; |
| | 20459 | 706 | | } |
| | | 707 | | else |
| | 2841 | 708 | | { |
| | 2841 | 709 | | Debug.Assert(EffectiveConverter.ElementType != null); |
| | 2841 | 710 | | potentialNumberType = EffectiveConverter.ElementType; |
| | 2841 | 711 | | } |
| | | 712 | | |
| | 23300 | 713 | | potentialNumberType = Nullable.GetUnderlyingType(potentialNumberType) ?? potentialNumberType; |
| | | 714 | | |
| | 23300 | 715 | | return potentialNumberType == typeof(byte) || |
| | 23300 | 716 | | potentialNumberType == typeof(decimal) || |
| | 23300 | 717 | | potentialNumberType == typeof(double) || |
| | 23300 | 718 | | potentialNumberType == typeof(short) || |
| | 23300 | 719 | | potentialNumberType == typeof(int) || |
| | 23300 | 720 | | potentialNumberType == typeof(long) || |
| | 23300 | 721 | | potentialNumberType == typeof(sbyte) || |
| | 23300 | 722 | | potentialNumberType == typeof(float) || |
| | 23300 | 723 | | potentialNumberType == typeof(ushort) || |
| | 23300 | 724 | | potentialNumberType == typeof(uint) || |
| | 23300 | 725 | | potentialNumberType == typeof(ulong) || |
| | 23300 | 726 | | #if NET |
| | 23300 | 727 | | potentialNumberType == typeof(Half) || |
| | 23300 | 728 | | #endif |
| | 23300 | 729 | | #if NET |
| | 23300 | 730 | | potentialNumberType == typeof(Int128) || |
| | 23300 | 731 | | potentialNumberType == typeof(UInt128) || |
| | 23300 | 732 | | #endif |
| | 23300 | 733 | | potentialNumberType == JsonTypeInfo.ObjectType; |
| | 32302 | 734 | | } |
| | | 735 | | |
| | | 736 | | /// <summary> |
| | | 737 | | /// Creates a <see cref="JsonPropertyInfo"/> instance whose type matches that of the current property. |
| | | 738 | | /// </summary> |
| | | 739 | | internal abstract void AddJsonParameterInfo(JsonParameterInfoValues parameterInfoValues); |
| | | 740 | | |
| | | 741 | | internal abstract bool GetMemberAndWriteJson(object obj, ref WriteStack state, Utf8JsonWriter writer); |
| | | 742 | | internal abstract bool GetMemberAndWriteJsonExtensionData(object obj, ref WriteStack state, Utf8JsonWriter write |
| | | 743 | | |
| | | 744 | | internal abstract object? GetValueAsObject(object obj); |
| | | 745 | | |
| | 20048 | 746 | | internal bool HasGetter => _untypedGet is not null; |
| | 20048 | 747 | | internal bool HasSetter => _untypedSet is not null; |
| | 0 | 748 | | internal bool IgnoreNullTokensOnRead { get; private protected set; } |
| | 0 | 749 | | internal bool IgnoreDefaultValuesOnWrite { get; private protected set; } |
| | | 750 | | |
| | | 751 | | internal bool IgnoreReadOnlyMember |
| | | 752 | | { |
| | | 753 | | get |
| | 0 | 754 | | { |
| | 0 | 755 | | Debug.Assert(MemberType == MemberTypes.Property || MemberType == MemberTypes.Field || MemberType == defa |
| | 0 | 756 | | return MemberType switch |
| | 0 | 757 | | { |
| | 0 | 758 | | MemberTypes.Property => Options.IgnoreReadOnlyProperties, |
| | 0 | 759 | | MemberTypes.Field => Options.IgnoreReadOnlyFields, |
| | 0 | 760 | | _ => false, |
| | 0 | 761 | | }; |
| | 0 | 762 | | } |
| | | 763 | | } |
| | | 764 | | |
| | | 765 | | /// <summary> |
| | | 766 | | /// True if the corresponding cref="JsonTypeInfo.PropertyInfoForTypeInfo"/> is this instance. |
| | | 767 | | /// </summary> |
| | 32306 | 768 | | internal bool IsForTypeInfo { get; init; } |
| | | 769 | | |
| | | 770 | | // There are 3 copies of the property name: |
| | | 771 | | // 1) Name. The unescaped property name. |
| | | 772 | | // 2) NameAsUtf8Bytes. The Utf8 version of Name. Used during deserialization for property lookup. |
| | | 773 | | // 3) EscapedNameSection. The escaped version of NameAsUtf8Bytes plus the wrapping quotes and a trailing colon. |
| | | 774 | | |
| | | 775 | | /// <summary> |
| | | 776 | | /// Gets or sets the JSON property name used when serializing the property. |
| | | 777 | | /// </summary> |
| | | 778 | | /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception> |
| | | 779 | | /// <exception cref="InvalidOperationException"> |
| | | 780 | | /// The <see cref="JsonPropertyInfo"/> instance has been locked for further modification. |
| | | 781 | | /// </exception> |
| | | 782 | | /// <remarks> |
| | | 783 | | /// The value of <see cref="Name"/> cannot conflict with that of other <see cref="JsonPropertyInfo"/> defined in |
| | | 784 | | /// |
| | | 785 | | /// For contracts originating from <see cref="DefaultJsonTypeInfoResolver"/> or <see cref="JsonSerializerContext |
| | | 786 | | /// the value typically reflects the underlying .NET member name, the name derived from <see cref="JsonSerialize |
| | | 787 | | /// or the value specified in <see cref="JsonPropertyNameAttribute" />. |
| | | 788 | | /// </remarks> |
| | | 789 | | public string Name |
| | | 790 | | { |
| | | 791 | | get |
| | 38970 | 792 | | { |
| | 38970 | 793 | | Debug.Assert(_name != null); |
| | 38970 | 794 | | return _name; |
| | 38970 | 795 | | } |
| | | 796 | | set |
| | 7794 | 797 | | { |
| | 7794 | 798 | | VerifyMutable(); |
| | | 799 | | |
| | 7794 | 800 | | ArgumentNullException.ThrowIfNull(value); |
| | | 801 | | |
| | 7794 | 802 | | _name = value; |
| | 7794 | 803 | | } |
| | | 804 | | } |
| | | 805 | | |
| | | 806 | | private string? _name; |
| | | 807 | | |
| | | 808 | | /// <summary> |
| | | 809 | | /// Utf8 version of Name. |
| | | 810 | | /// </summary> |
| | 93134 | 811 | | internal byte[] NameAsUtf8Bytes { get; private set; } = null!; |
| | | 812 | | |
| | | 813 | | /// <summary> |
| | | 814 | | /// The escaped name passed to the writer. |
| | | 815 | | /// </summary> |
| | 27842 | 816 | | internal byte[] EscapedNameSection { get; private set; } = null!; |
| | | 817 | | |
| | | 818 | | /// <summary> |
| | | 819 | | /// Gets the <see cref="JsonSerializerOptions"/> value associated with the current contract instance. |
| | | 820 | | /// </summary> |
| | 143395 | 821 | | public JsonSerializerOptions Options { get; } |
| | | 822 | | |
| | | 823 | | /// <summary> |
| | | 824 | | /// Gets or sets the serialization order for the current property. |
| | | 825 | | /// </summary> |
| | | 826 | | /// <exception cref="InvalidOperationException"> |
| | | 827 | | /// The <see cref="JsonPropertyInfo"/> instance has been locked for further modification. |
| | | 828 | | /// </exception> |
| | | 829 | | /// <remarks> |
| | | 830 | | /// For contracts originating from <see cref="DefaultJsonTypeInfoResolver"/> or <see cref="JsonSerializerContext |
| | | 831 | | /// the value of this property will be mapped from <see cref="JsonPropertyOrderAttribute"/> annotations. |
| | | 832 | | /// </remarks> |
| | | 833 | | public int Order |
| | | 834 | | { |
| | 23382 | 835 | | get => _order; |
| | | 836 | | set |
| | 7794 | 837 | | { |
| | 7794 | 838 | | VerifyMutable(); |
| | 7794 | 839 | | _order = value; |
| | 7794 | 840 | | } |
| | | 841 | | } |
| | | 842 | | |
| | | 843 | | private int _order; |
| | | 844 | | |
| | | 845 | | internal bool ReadJsonAndAddExtensionProperty( |
| | | 846 | | object obj, |
| | | 847 | | scoped ref ReadStack state, |
| | | 848 | | ref Utf8JsonReader reader) |
| | 0 | 849 | | { |
| | 0 | 850 | | object propValue = GetValueAsObject(obj)!; |
| | | 851 | | |
| | 0 | 852 | | if (propValue is IDictionary<string, object?> dictionaryObjectValue) |
| | 0 | 853 | | { |
| | 0 | 854 | | if (reader.TokenType == JsonTokenType.Null) |
| | 0 | 855 | | { |
| | | 856 | | // A null JSON value is treated as a null object reference. |
| | 0 | 857 | | AddProperty(in state.Current, dictionaryObjectValue, null); |
| | 0 | 858 | | } |
| | | 859 | | else |
| | 0 | 860 | | { |
| | 0 | 861 | | JsonConverter<object> converter = GetDictionaryValueConverter<object>(); |
| | 0 | 862 | | object value = converter.Read(ref reader, JsonTypeInfo.ObjectType, Options)!; |
| | 0 | 863 | | AddProperty(in state.Current, dictionaryObjectValue, value); |
| | 0 | 864 | | } |
| | 0 | 865 | | } |
| | 0 | 866 | | else if (propValue is IDictionary<string, JsonElement> dictionaryElementValue) |
| | 0 | 867 | | { |
| | 0 | 868 | | JsonConverter<JsonElement> converter = GetDictionaryValueConverter<JsonElement>(); |
| | 0 | 869 | | JsonElement value = converter.Read(ref reader, typeof(JsonElement), Options); |
| | 0 | 870 | | AddProperty(in state.Current, dictionaryElementValue, value); |
| | 0 | 871 | | } |
| | | 872 | | else |
| | 0 | 873 | | { |
| | | 874 | | // Avoid a type reference to JsonObject and its converter to support trimming. |
| | 0 | 875 | | Debug.Assert(propValue is Nodes.JsonObject); |
| | 0 | 876 | | EffectiveConverter.ReadElementAndSetProperty(propValue, state.Current.JsonPropertyNameAsString!, ref rea |
| | 0 | 877 | | } |
| | | 878 | | |
| | 0 | 879 | | return true; |
| | | 880 | | |
| | | 881 | | JsonConverter<TValue> GetDictionaryValueConverter<TValue>() |
| | 0 | 882 | | { |
| | 0 | 883 | | JsonTypeInfo dictionaryValueInfo = |
| | 0 | 884 | | JsonTypeInfo.ElementTypeInfo |
| | 0 | 885 | | // Slower path for non-generic types that implement IDictionary<,>. |
| | 0 | 886 | | // It is possible to cache this converter on JsonTypeInfo if we assume the property value |
| | 0 | 887 | | // will always be the same type for all instances. |
| | 0 | 888 | | ?? Options.GetTypeInfoInternal(typeof(TValue)); |
| | | 889 | | |
| | 0 | 890 | | Debug.Assert(dictionaryValueInfo is JsonTypeInfo<TValue>); |
| | 0 | 891 | | return ((JsonTypeInfo<TValue>)dictionaryValueInfo).EffectiveConverter; |
| | 0 | 892 | | } |
| | | 893 | | |
| | | 894 | | void AddProperty<TValue>(ref readonly ReadStackFrame current, IDictionary<string, TValue> d, TValue value) |
| | 0 | 895 | | { |
| | 0 | 896 | | string property = current.JsonPropertyNameAsString!; |
| | 0 | 897 | | if (Options.AllowDuplicateProperties) |
| | 0 | 898 | | { |
| | 0 | 899 | | d[property] = value; |
| | 0 | 900 | | } |
| | | 901 | | else |
| | 0 | 902 | | { |
| | | 903 | | #if NET |
| | 0 | 904 | | if (!d.TryAdd(property, value)) |
| | | 905 | | #else |
| | | 906 | | if (d.ContainsKey(property)) |
| | | 907 | | #endif |
| | 0 | 908 | | { |
| | 0 | 909 | | ThrowHelper.ThrowJsonException_DuplicatePropertyNotAllowed(current.JsonPropertyInfo!); |
| | | 910 | | } |
| | | 911 | | |
| | | 912 | | #if !NET |
| | | 913 | | d[property] = value; |
| | | 914 | | #endif |
| | 0 | 915 | | } |
| | 0 | 916 | | } |
| | 0 | 917 | | } |
| | | 918 | | |
| | | 919 | | internal abstract bool ReadJsonAndSetMember(object obj, scoped ref ReadStack state, ref Utf8JsonReader reader); |
| | | 920 | | |
| | | 921 | | internal abstract bool ReadJsonAsObject(scoped ref ReadStack state, ref Utf8JsonReader reader, out object? value |
| | | 922 | | |
| | | 923 | | internal bool ReadJsonExtensionDataValue(scoped ref ReadStack state, ref Utf8JsonReader reader, out object? valu |
| | 0 | 924 | | { |
| | 0 | 925 | | Debug.Assert(this == state.Current.JsonTypeInfo.ExtensionDataProperty); |
| | | 926 | | |
| | 0 | 927 | | if (JsonTypeInfo.ElementType == JsonTypeInfo.ObjectType && reader.TokenType == JsonTokenType.Null) |
| | 0 | 928 | | { |
| | 0 | 929 | | value = null; |
| | 0 | 930 | | return true; |
| | | 931 | | } |
| | | 932 | | |
| | 0 | 933 | | JsonConverter<JsonElement> converter = (JsonConverter<JsonElement>)Options.GetConverterInternal(typeof(JsonE |
| | 0 | 934 | | if (!converter.TryRead(ref reader, typeof(JsonElement), Options, ref state, out JsonElement jsonElement, out |
| | 0 | 935 | | { |
| | | 936 | | // JsonElement is a struct that must be read in full. |
| | 0 | 937 | | value = null; |
| | 0 | 938 | | return false; |
| | | 939 | | } |
| | | 940 | | |
| | 0 | 941 | | value = jsonElement; |
| | 0 | 942 | | return true; |
| | 0 | 943 | | } |
| | | 944 | | |
| | | 945 | | internal void EnsureChildOf(JsonTypeInfo parent) |
| | 7794 | 946 | | { |
| | 7794 | 947 | | if (DeclaringTypeInfo is null) |
| | 0 | 948 | | { |
| | 0 | 949 | | DeclaringTypeInfo = parent; |
| | 0 | 950 | | } |
| | 7794 | 951 | | else if (DeclaringTypeInfo != parent) |
| | 0 | 952 | | { |
| | 0 | 953 | | ThrowHelper.ThrowInvalidOperationException_JsonPropertyInfoIsBoundToDifferentJsonTypeInfo(this); |
| | | 954 | | } |
| | | 955 | | |
| | 7794 | 956 | | DeclaringTypeInfo.ResolveMatchingParameterInfo(this); |
| | 7794 | 957 | | } |
| | | 958 | | |
| | | 959 | | /// <summary> |
| | | 960 | | /// Tries to get pre-populated value from the property if populating is enabled. |
| | | 961 | | /// If property value is <see langword="null"/> this method will return false. |
| | | 962 | | /// </summary> |
| | | 963 | | internal bool TryGetPrePopulatedValue(scoped ref ReadStack state) |
| | 0 | 964 | | { |
| | 0 | 965 | | if (EffectiveObjectCreationHandling != JsonObjectCreationHandling.Populate) |
| | 0 | 966 | | return false; |
| | | 967 | | |
| | 0 | 968 | | Debug.Assert(EffectiveConverter.CanPopulate, "Property is marked with Populate but converter cannot populate |
| | 0 | 969 | | Debug.Assert(state.Parent.ReturnValue != null, "Parent object is null"); |
| | 0 | 970 | | Debug.Assert(!state.Current.IsPopulating, "We've called TryGetPrePopulatedValue more than once"); |
| | 0 | 971 | | object? value = Get!(state.Parent.ReturnValue); |
| | 0 | 972 | | state.Current.ReturnValue = value; |
| | 0 | 973 | | state.Current.IsPopulating = value != null; |
| | 0 | 974 | | return value != null; |
| | 0 | 975 | | } |
| | | 976 | | |
| | | 977 | | internal JsonTypeInfo JsonTypeInfo |
| | | 978 | | { |
| | | 979 | | get |
| | 7798 | 980 | | { |
| | 7798 | 981 | | Debug.Assert(_jsonTypeInfo?.IsConfigurationStarted == true); |
| | | 982 | | // Even though this instance has already been configured, |
| | | 983 | | // it is possible for contending threads to call the property |
| | | 984 | | // while the wider JsonTypeInfo graph is still being configured. |
| | | 985 | | // Call EnsureConfigured() to force synchronization if necessary. |
| | 7798 | 986 | | JsonTypeInfo jsonTypeInfo = _jsonTypeInfo; |
| | 7798 | 987 | | jsonTypeInfo.EnsureConfigured(); |
| | 7798 | 988 | | return jsonTypeInfo; |
| | 7798 | 989 | | } |
| | | 990 | | set |
| | 18691 | 991 | | { |
| | 18691 | 992 | | _jsonTypeInfo = value; |
| | 18691 | 993 | | } |
| | | 994 | | } |
| | | 995 | | |
| | | 996 | | private JsonTypeInfo? _jsonTypeInfo; |
| | | 997 | | |
| | | 998 | | /// <summary> |
| | | 999 | | /// Returns true if <see cref="JsonTypeInfo"/> has been configured. |
| | | 1000 | | /// This might be false even if <see cref="IsConfigured"/> is true |
| | | 1001 | | /// in cases of recursive types or <see cref="IsIgnored"/> is true. |
| | | 1002 | | /// </summary> |
| | 7794 | 1003 | | internal bool IsPropertyTypeInfoConfigured => _jsonTypeInfo?.IsConfigured == true; |
| | | 1004 | | |
| | | 1005 | | /// <summary> |
| | | 1006 | | /// Property was marked JsonIgnoreCondition.Always and also hasn't been configured by the user. |
| | | 1007 | | /// </summary> |
| | 27842 | 1008 | | internal bool IsIgnored => _ignoreCondition is JsonIgnoreCondition.Always && Get is null && Set is null; |
| | | 1009 | | |
| | | 1010 | | /// <summary> |
| | | 1011 | | /// Reflects the value of <see cref="HasGetter"/> combined with any additional global ignore policies. |
| | | 1012 | | /// </summary> |
| | 20048 | 1013 | | internal bool CanSerialize { get; private set; } |
| | | 1014 | | /// <summary> |
| | | 1015 | | /// Reflects the value of <see cref="HasSetter"/> combined with any additional global ignore policies. |
| | | 1016 | | /// </summary> |
| | 40096 | 1017 | | internal bool CanDeserialize { get; private set; } |
| | | 1018 | | |
| | | 1019 | | /// <summary> |
| | | 1020 | | /// Reflects the value can be deserialized or populated |
| | | 1021 | | /// </summary> |
| | 20048 | 1022 | | internal bool CanDeserializeOrPopulate { get; private set; } |
| | | 1023 | | |
| | | 1024 | | /// <summary> |
| | | 1025 | | /// Relevant to source generated metadata: did the property have the <see cref="JsonIncludeAttribute"/>? |
| | | 1026 | | /// </summary> |
| | 0 | 1027 | | internal bool SrcGen_HasJsonInclude { get; set; } |
| | | 1028 | | |
| | | 1029 | | /// <summary> |
| | | 1030 | | /// Relevant to source generated metadata: is the property public? |
| | | 1031 | | /// </summary> |
| | 0 | 1032 | | internal bool SrcGen_IsPublic { get; set; } |
| | | 1033 | | |
| | | 1034 | | /// <summary> |
| | | 1035 | | /// Gets or sets the <see cref="JsonNumberHandling"/> applied to the current property. |
| | | 1036 | | /// </summary> |
| | | 1037 | | /// <exception cref="InvalidOperationException"> |
| | | 1038 | | /// The <see cref="JsonPropertyInfo"/> instance has been locked for further modification. |
| | | 1039 | | /// </exception> |
| | | 1040 | | /// <remarks> |
| | | 1041 | | /// For contracts originating from <see cref="DefaultJsonTypeInfoResolver"/> or <see cref="JsonSerializerContext |
| | | 1042 | | /// the value of this property will be mapped from <see cref="JsonNumberHandlingAttribute"/> annotations. |
| | | 1043 | | /// </remarks> |
| | | 1044 | | public JsonNumberHandling? NumberHandling |
| | | 1045 | | { |
| | 20048 | 1046 | | get => _numberHandling; |
| | | 1047 | | set |
| | 7794 | 1048 | | { |
| | 7794 | 1049 | | VerifyMutable(); |
| | 7794 | 1050 | | _numberHandling = value; |
| | 7794 | 1051 | | } |
| | | 1052 | | } |
| | | 1053 | | |
| | | 1054 | | private JsonNumberHandling? _numberHandling; |
| | | 1055 | | |
| | | 1056 | | /// <summary> |
| | | 1057 | | /// Number handling after considering options and declaring type number handling |
| | | 1058 | | /// </summary> |
| | 75147 | 1059 | | internal JsonNumberHandling? EffectiveNumberHandling { get; private set; } |
| | | 1060 | | |
| | | 1061 | | // Whether the property type can be null. |
| | | 1062 | | internal abstract bool PropertyTypeCanBeNull { get; } |
| | | 1063 | | |
| | | 1064 | | /// <summary> |
| | | 1065 | | /// Default value used for parameterized ctor invocation. |
| | | 1066 | | /// </summary> |
| | | 1067 | | internal abstract object? DefaultValue { get; } |
| | | 1068 | | |
| | | 1069 | | /// <summary> |
| | | 1070 | | /// Property index on the list of JsonTypeInfo properties. |
| | | 1071 | | /// It is used as a unique identifier for properties. |
| | | 1072 | | /// It is set just before property is configured and does not change afterward. |
| | | 1073 | | /// It is not equivalent to index on the properties list |
| | | 1074 | | /// </summary> |
| | | 1075 | | [DebuggerBrowsable(DebuggerBrowsableState.Never)] |
| | | 1076 | | internal int PropertyIndex |
| | | 1077 | | { |
| | | 1078 | | get |
| | 0 | 1079 | | { |
| | 0 | 1080 | | Debug.Assert(IsConfigured); |
| | 0 | 1081 | | return _propertyIndex; |
| | 0 | 1082 | | } |
| | | 1083 | | set |
| | 7794 | 1084 | | { |
| | 7794 | 1085 | | Debug.Assert(!IsConfigured); |
| | 7794 | 1086 | | _propertyIndex = value; |
| | 7794 | 1087 | | } |
| | | 1088 | | } |
| | | 1089 | | |
| | | 1090 | | private int _propertyIndex; |
| | | 1091 | | |
| | | 1092 | | internal bool IsOverriddenOrShadowedBy(JsonPropertyInfo other) |
| | 0 | 1093 | | => MemberName == other.MemberName && DeclaringType.IsAssignableFrom(other.DeclaringType); |
| | | 1094 | | |
| | | 1095 | | [DebuggerBrowsable(DebuggerBrowsableState.Never)] |
| | 0 | 1096 | | private string DebuggerDisplay => $"Name = {Name}, PropertyType = {PropertyType}"; |
| | | 1097 | | } |
| | | 1098 | | } |