< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 7
Coverable lines: 7
Total lines: 85
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Attributes/JsonPolymorphicAttribute.cs

#LineLine coverage
 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
 4using System.Diagnostics.CodeAnalysis;
 5
 6namespace System.Text.Json.Serialization
 7{
 8    /// <summary>
 9    /// When placed on a type, indicates that the type should be serialized polymorphically.
 10    /// </summary>
 11    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, AllowMultiple = false, Inherited = false)]
 12    public sealed class JsonPolymorphicAttribute : JsonAttribute
 13    {
 14        /// <summary>
 15        /// Gets or sets a custom type discriminator property name for the polymorphic type.
 16        /// Uses the default '$type' property name if left unset.
 17        /// </summary>
 018        public string? TypeDiscriminatorPropertyName { get; set; }
 19
 20        /// <summary>
 21        /// Gets or sets the behavior when serializing an undeclared derived runtime type.
 22        /// </summary>
 023        public JsonUnknownDerivedTypeHandling UnknownDerivedTypeHandling { get; set; }
 24
 25        /// <summary>
 26        /// Gets or sets a value that indicates whether derived type registrations should be inferred
 27        /// from compiler-provided metadata for a closed type hierarchy.
 28        /// </summary>
 29        /// <value>
 30        /// <see langword="true"/> to infer derived type registrations; otherwise, <see langword="false"/>.
 31        /// The default is <see langword="false"/>.
 32        /// </value>
 33        /// <remarks>
 34        /// Setting this property overrides <see cref="JsonSerializerOptions.InferClosedTypePolymorphism"/>
 35        /// for the annotated type, so an explicit <see langword="false"/> suppresses inference even when it is
 36        /// enabled globally. When the property is left unset the globally configured value is used.
 37        /// If the annotated type declares one or more <see cref="JsonDerivedTypeAttribute"/> registrations,
 38        /// inference is skipped for that type and only the explicitly registered derived types are used.
 39        /// Closed derived types are expanded recursively and only terminal derived types are registered.
 40        /// Polymorphism configuration declared on derived types applies to their respective contracts and does
 41        /// not affect inference for the annotated type.
 42        /// </remarks>
 43        public bool InferClosedTypePolymorphism
 44        {
 045            get => _inferClosedTypePolymorphism ?? false;
 046            set => _inferClosedTypePolymorphism = value;
 47        }
 48
 49        /// <summary>
 50        /// Gets the explicitly configured <see cref="InferClosedTypePolymorphism"/> value,
 51        /// or <see langword="null"/> if the property has not been set.
 52        /// </summary>
 053        internal bool? InferClosedTypePolymorphismOrNull => _inferClosedTypePolymorphism;
 54
 55        private bool? _inferClosedTypePolymorphism;
 56
 57        /// <summary>
 58        /// When set to <see langword="true"/>, instructs the deserializer to ignore any
 59        /// unrecognized type discriminator id's and reverts to the contract of the base type.
 60        /// Otherwise, it will fail the deserialization.
 61        /// </summary>
 062        public bool IgnoreUnrecognizedTypeDiscriminators { get; set; }
 63
 64        /// <summary>
 65        /// Gets or sets the type of a <see cref="JsonTypeClassifierFactory"/> implementation
 66        /// used to classify JSON payloads during deserialization instead of relying on
 67        /// the standard type discriminator property.
 68        /// </summary>
 69        /// <remarks>
 70        /// <para>
 71        /// When set, the classifier is invoked before any discriminator-based resolution.
 72        /// The classifier receives a <see cref="Utf8JsonReader"/> positioned at the start of
 73        /// the JSON object and returns the resolved <see cref="Type"/>. Returning
 74        /// <see langword="null"/> fails deserialization.
 75        /// </para>
 76        /// <para>
 77        /// The specified type must derive from <see cref="JsonTypeClassifierFactory"/>
 78        /// and have a public parameterless constructor.
 79        /// </para>
 80        /// </remarks>
 81        [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
 082        public Type? TypeClassifier { get; set; }
 83    }
 84}
 85