< Summary

Information
Class: System.Text.Json.Serialization.Converters.UnsupportedTypeConverterFactory
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\Converters\Value\UnsupportedTypeConverterFactory.cs
Line coverage
53%
Covered lines: 15
Uncovered lines: 13
Coverable lines: 28
Total lines: 51
Line coverage: 53.5%
Branch coverage
50%
Covered branches: 4
Total branches: 8
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
CanConvert(...)50%88100%
CreateConverter(...)100%110%
CreateUnsupportedConverterForType(...)100%110%

File(s)

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\Converters\Value\UnsupportedTypeConverterFactory.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;
 5using System.Diagnostics.CodeAnalysis;
 6using System.Reflection;
 7using System.Runtime.Serialization;
 8
 9namespace System.Text.Json.Serialization.Converters
 10{
 11    [RequiresDynamicCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)]
 12    internal sealed class UnsupportedTypeConverterFactory : JsonConverterFactory
 13    {
 14        public override bool CanConvert(Type type)
 364615        {
 16            // If a type is added, also add to the SourceGeneration project.
 17
 364618            return
 364619                // There's no safe way to construct a Type/MemberInfo from untrusted user input.
 364620                typeof(MemberInfo).IsAssignableFrom(type) ||
 364621                // (De)serialization of SerializationInfo is already disallowed due to Type being disallowed
 364622                // (the two ctors on SerializationInfo take a Type, and a Type member is present when serializing).
 364623                // Explicitly disallowing this type provides a clear exception when ctors with
 364624                // .ctor(SerializationInfo, StreamingContext) signatures are attempted to be used for deserialization.
 364625                // Invoking such ctors is not safe when used with untrusted user input.
 364626                type == typeof(SerializationInfo) ||
 364627                type == typeof(IntPtr) ||
 364628                type == typeof(UIntPtr) ||
 364629                // Exclude delegates.
 364630                typeof(Delegate).IsAssignableFrom(type);
 364631        }
 32
 33        public override JsonConverter CreateConverter(Type type, JsonSerializerOptions options)
 034        {
 035            Debug.Assert(CanConvert(type));
 036            return CreateUnsupportedConverterForType(type);
 037        }
 38
 39        internal static JsonConverter CreateUnsupportedConverterForType(Type type, string? errorMessage = null)
 040        {
 041            JsonConverter converter = (JsonConverter)Activator.CreateInstance(
 042                typeof(UnsupportedTypeConverter<>).MakeGenericType(type),
 043                BindingFlags.Instance | BindingFlags.Public,
 044                binder: null,
 045                args: new object?[] { errorMessage },
 046                culture: null)!;
 47
 048            return converter;
 049        }
 50    }
 51}