< Summary

Information
Class: System.Text.Json.Serialization.Metadata.JsonPolymorphismOptions
Assembly: System.Text.Json
File(s): C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\Metadata\JsonPolymorphismOptions.cs
Line coverage
13%
Covered lines: 6
Uncovered lines: 38
Coverable lines: 44
Total lines: 123
Line coverage: 13.6%
Branch coverage
12%
Covered branches: 2
Total branches: 16
Branch coverage: 12.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%110%
VerifyMutable()0%220%
.ctor(...)100%110%
OnCollectionModifying()0%220%
CreateFromAttributeDeclarations(...)33.33%6635.29%

File(s)

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\Metadata\JsonPolymorphismOptions.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.Collections.Generic;
 5using System.Diagnostics.CodeAnalysis;
 6using System.Reflection;
 7
 8namespace System.Text.Json.Serialization.Metadata
 9{
 10    /// <summary>
 11    /// Defines polymorphic configuration for a specified base type.
 12    /// </summary>
 13    public class JsonPolymorphismOptions
 14    {
 15        private DerivedTypeList? _derivedTypes;
 16        private bool _ignoreUnrecognizedTypeDiscriminators;
 17        private JsonUnknownDerivedTypeHandling _unknownDerivedTypeHandling;
 18        private string? _typeDiscriminatorPropertyName;
 19
 20        /// <summary>
 21        /// Creates an empty <see cref="JsonPolymorphismOptions"/> instance.
 22        /// </summary>
 023        public JsonPolymorphismOptions()
 024        {
 025        }
 26
 27        /// <summary>
 28        /// Gets the list of derived types supported in the current polymorphic type configuration.
 29        /// </summary>
 030        public IList<JsonDerivedType> DerivedTypes => _derivedTypes ??= new(this);
 31
 32        /// <summary>
 33        /// When set to <see langword="true"/>, instructs the serializer to ignore any
 34        /// unrecognized type discriminator id's and reverts to the contract of the base type.
 35        /// Otherwise, it will fail the deserialization.
 36        /// </summary>
 37        /// <exception cref="InvalidOperationException">
 38        /// The parent <see cref="JsonTypeInfo"/> instance has been locked for further modification.
 39        /// </exception>
 40        public bool IgnoreUnrecognizedTypeDiscriminators
 41        {
 042            get => _ignoreUnrecognizedTypeDiscriminators;
 43            set
 044            {
 045                VerifyMutable();
 046                _ignoreUnrecognizedTypeDiscriminators = value;
 047            }
 48        }
 49
 50        /// <summary>
 51        /// Gets or sets the behavior when serializing an undeclared derived runtime type.
 52        /// </summary>
 53        /// <exception cref="InvalidOperationException">
 54        /// The parent <see cref="JsonTypeInfo"/> instance has been locked for further modification.
 55        /// </exception>
 56        public JsonUnknownDerivedTypeHandling UnknownDerivedTypeHandling
 57        {
 058            get => _unknownDerivedTypeHandling;
 59            set
 060            {
 061                VerifyMutable();
 062                _unknownDerivedTypeHandling = value;
 063            }
 64        }
 65
 66        /// <summary>
 67        /// Gets or sets a custom type discriminator property name for the polymorhic type.
 68        /// Uses the default '$type' property name if left unset.
 69        /// </summary>
 70        /// <exception cref="InvalidOperationException">
 71        /// The parent <see cref="JsonTypeInfo"/> instance has been locked for further modification.
 72        /// </exception>
 73        [AllowNull]
 74        public string TypeDiscriminatorPropertyName
 75        {
 076            get => _typeDiscriminatorPropertyName ?? JsonSerializer.TypePropertyName;
 77            set
 078            {
 079                VerifyMutable();
 080                _typeDiscriminatorPropertyName = value;
 081            }
 82        }
 83
 084        private void VerifyMutable() => DeclaringTypeInfo?.VerifyMutable();
 85
 086        internal JsonTypeInfo? DeclaringTypeInfo { get; set; }
 87
 88        private sealed class DerivedTypeList : ConfigurationList<JsonDerivedType>
 89        {
 90            private readonly JsonPolymorphismOptions _parent;
 91
 092            public DerivedTypeList(JsonPolymorphismOptions parent)
 093            {
 094                _parent = parent;
 095            }
 96
 097            public override bool IsReadOnly => _parent.DeclaringTypeInfo?.IsReadOnly == true;
 098            protected override void OnCollectionModifying() => _parent.DeclaringTypeInfo?.VerifyMutable();
 99        }
 100
 101        internal static JsonPolymorphismOptions? CreateFromAttributeDeclarations(Type baseType)
 12254102        {
 12254103            JsonPolymorphismOptions? options = null;
 104
 12254105            if (baseType.GetCustomAttribute<JsonPolymorphicAttribute>(inherit: false) is JsonPolymorphicAttribute polymo
 0106            {
 0107                options = new()
 0108                {
 0109                    IgnoreUnrecognizedTypeDiscriminators = polymorphicAttribute.IgnoreUnrecognizedTypeDiscriminators,
 0110                    UnknownDerivedTypeHandling = polymorphicAttribute.UnknownDerivedTypeHandling,
 0111                    TypeDiscriminatorPropertyName = polymorphicAttribute.TypeDiscriminatorPropertyName,
 0112                };
 0113            }
 114
 36762115            foreach (JsonDerivedTypeAttribute attr in baseType.GetCustomAttributes<JsonDerivedTypeAttribute>(inherit: fa
 0116            {
 0117                (options ??= new()).DerivedTypes.Add(new JsonDerivedType(attr.DerivedType, attr.TypeDiscriminator));
 0118            }
 119
 12254120            return options;
 12254121        }
 122    }
 123}