< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 1
Coverable lines: 1
Total lines: 62
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/JsonUnionAttribute.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    /// Customizes the System.Text.Json behavior of a union type.
 10    /// </summary>
 11    /// <remarks>
 12    /// <para>
 13    /// A type is recognized by System.Text.Json as a union when it carries
 14    /// <c>System.Runtime.CompilerServices.UnionAttribute</c> (the marker emitted by
 15    /// the C# compiler for union declarations). Applying
 16    /// <see cref="JsonUnionAttribute"/> on its own does not make a type a union — the
 17    /// attribute is silently ignored when no union marker is present, and the
 18    /// type is serialized as a regular object.
 19    /// </para>
 20    /// <para>
 21    /// On a recognized union, this attribute is purely a customization knob — currently
 22    /// it lets the user plug in a per-type <see cref="TypeClassifier"/>. Recognized unions
 23    /// without this attribute receive the default JSON-value-based classifier.
 24    /// </para>
 25    /// <para>
 26    /// Case types are discovered automatically from the union's public single-parameter
 27    /// constructors. For types where convention-based discovery does not work, use
 28    /// contract customization to populate <see cref="Metadata.JsonTypeInfo.UnionCases"/>
 29    /// and set the deconstructor/constructor delegates on
 30    /// <see cref="Metadata.JsonTypeInfo{T}"/>.
 31    /// </para>
 32    /// </remarks>
 33    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false, Inherited = false)]
 34    public sealed class JsonUnionAttribute : JsonAttribute
 35    {
 36        /// <summary>
 37        /// Gets or sets the type of a <see cref="JsonTypeClassifierFactory"/> implementation
 38        /// used to classify JSON payloads during deserialization.
 39        /// </summary>
 40        /// <remarks>
 41        /// <para>
 42        /// When <see langword="null"/>, the built-in JSON value-shape classifier is used.
 43        /// It dispatches using the current JSON token kind rather than by inspecting object
 44        /// property names or other payload contents.
 45        /// </para>
 46        /// <para>
 47        /// The specified type must derive from <see cref="JsonTypeClassifierFactory"/>
 48        /// and have a public parameterless constructor.
 49        /// </para>
 50        /// <para>
 51        /// The classifier is not invoked for JSON <see cref="JsonTokenType.Null"/>
 52        /// payloads. The null token is dispatched directly to the union's constructor delegate,
 53        /// which yields the canonical null union when at least one case is nullable
 54        /// and otherwise throws a <see cref="JsonException"/>. See
 55        /// <see cref="JsonTypeClassifier"/> for details.
 56        /// </para>
 57        /// </remarks>
 58        [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
 059        public Type? TypeClassifier { get; set; }
 60    }
 61}
 62

Methods/Properties

TypeClassifier()