| | | 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.Text.Json.Serialization.Metadata; |
| | | 6 | | |
| | | 7 | | namespace System.Text.Json.Serialization; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Determines how deserialization will handle object creation for fields or properties. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// When placed on a field or property, indicates if member will replaced or populated. |
| | | 14 | | /// When default resolvers are used this will be mapped to <see cref="JsonPropertyInfo.ObjectCreationHandling"/>. |
| | | 15 | | /// |
| | | 16 | | /// When placed on a type with <see cref="JsonObjectCreationHandling.Populate"/> indicates that all members |
| | | 17 | | /// that support population will be populated. When default resolvers are used this will be mapped to <see cref="JsonTyp |
| | | 18 | | /// |
| | | 19 | | /// Note that the attribute corresponds only to the preferred values of creation handling for properties when placed on |
| | | 20 | | /// For example when <see cref="JsonObjectCreationHandlingAttribute"/> with <see cref="JsonObjectCreationHandling.Popula |
| | | 21 | | /// and property is not capable of being populated, it will be replaced. |
| | | 22 | | /// That may be true if i.e. value type doesn't have a setter or property is of type <see cref="IEnumerable{T}"/>. |
| | | 23 | | /// |
| | | 24 | | /// Only the property type is taken into consideration. For example if property is of type |
| | | 25 | | /// <see cref="IEnumerable{T}"/> has runtime value of type <see cref="List{T}"/> it will not be populated |
| | | 26 | | /// because <see cref="IEnumerable{T}"/> is not capable of populating. |
| | | 27 | | /// |
| | | 28 | | /// Value types require a setter to support population; in such cases |
| | | 29 | | /// deserialization will use a copy of the property value which will get assigned back to the setter once finished. |
| | | 30 | | /// </remarks> |
| | | 31 | | [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Class | AttributeTargets.Struct | |
| | | 32 | | public sealed class JsonObjectCreationHandlingAttribute : JsonAttribute |
| | | 33 | | { |
| | | 34 | | /// <summary> |
| | | 35 | | /// Indicates what configuration should be used when deserializing members. |
| | | 36 | | /// </summary> |
| | 0 | 37 | | public JsonObjectCreationHandling Handling { get; } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Initializes a new instance of <see cref="JsonObjectCreationHandlingAttribute"/>. |
| | | 41 | | /// </summary> |
| | | 42 | | /// <param name="handling">The handling to apply to the current member.</param> |
| | 0 | 43 | | public JsonObjectCreationHandlingAttribute(JsonObjectCreationHandling handling) |
| | 0 | 44 | | { |
| | 0 | 45 | | if (!JsonSerializer.IsValidCreationHandlingValue(handling)) |
| | 0 | 46 | | { |
| | 0 | 47 | | throw new ArgumentOutOfRangeException(nameof(handling)); |
| | | 48 | | } |
| | | 49 | | |
| | 0 | 50 | | Handling = handling; |
| | 0 | 51 | | } |
| | | 52 | | } |