< Summary

Line coverage
31%
Covered lines: 5
Uncovered lines: 11
Coverable lines: 16
Total lines: 45
Line coverage: 31.2%
Branch coverage
16%
Covered branches: 1
Total branches: 6
Branch coverage: 16.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
CanConvert(...)100%11100%
CreateConverter(...)100%110%
IsUnionType(...)16.66%6636.36%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Union/JsonUnionConverterFactory.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;
 5using System.Reflection;
 6
 7namespace System.Text.Json.Serialization.Converters
 8{
 9    [RequiresDynamicCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)]
 10    [RequiresUnreferencedCode(JsonSerializer.SerializationUnreferencedCodeMessage)]
 11    internal sealed class JsonUnionConverterFactory : JsonConverterFactory
 12    {
 13        private const string UnionAttributeName = "UnionAttribute";
 14        private const string UnionAttributeFullName = "System.Runtime.CompilerServices.UnionAttribute";
 15
 16        // A type IS a union iff it carries [System.Runtime.CompilerServices.UnionAttribute].
 17        // The marker is detected by full name (rather than via a compile-time reference)
 18        // because the C# compiler may polyfill it into the user's assembly when targeting a
 19        // downlevel runtime. [JsonUnion] alone is a customization attribute that has no meaning
 20        // on non-union types.
 49621        public override bool CanConvert(Type typeToConvert) => IsUnionType(typeToConvert);
 22
 23        public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
 024        {
 025            Type converterType = typeof(JsonUnionConverter<>).MakeGenericType(typeToConvert);
 026            return (JsonConverter)Activator.CreateInstance(converterType)!;
 027        }
 28
 29        private static bool IsUnionType(Type type)
 49630        {
 148831            foreach (CustomAttributeData data in type.GetCustomAttributesData())
 032            {
 033                Type attributeType = data.AttributeType;
 034                if (attributeType.Name == UnionAttributeName &&
 035                    attributeType.FullName == UnionAttributeFullName)
 036                {
 037                    return true;
 38                }
 039            }
 40
 49641            return false;
 49642        }
 43    }
 44}
 45