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