< Summary

Information
Class: System.Text.Json.Serialization.Converters.CastingConverter<T>
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\CastingConverter.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 62
Coverable lines: 62
Total lines: 135
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 8
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%110%
Read(...)100%110%
Write(...)100%110%
OnTryRead(...)100%110%
OnTryWrite(...)100%110%
ReadAsPropertyName(...)0%220%
ReadAsPropertyNameCore(...)0%220%
WriteAsPropertyName(...)0%220%
WriteAsPropertyNameCore(...)0%220%
ReadNumberWithCustomHandling(...)100%110%
WriteNumberWithCustomHandling(...)100%110%
GetSchema(...)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\CastingConverter.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.Text.Json.Nodes;
 7using System.Text.Json.Reflection;
 8using System.Text.Json.Schema;
 9
 10namespace System.Text.Json.Serialization.Converters
 11{
 12    /// <summary>
 13    /// Converter wrapper which casts SourceType into TargetType
 14    /// </summary>
 15    internal sealed class CastingConverter<T> : JsonConverter<T>
 16    {
 17        private readonly JsonConverter _sourceConverter;
 018        internal override Type? KeyType => _sourceConverter.KeyType;
 019        internal override Type? ElementType => _sourceConverter.ElementType;
 020        internal override JsonConverter? NullableElementConverter => _sourceConverter.NullableElementConverter;
 21
 022        public override bool HandleNull { get; }
 023        internal override bool SupportsCreateObjectDelegate => _sourceConverter.SupportsCreateObjectDelegate;
 24
 025        internal CastingConverter(JsonConverter sourceConverter)
 026        {
 027            Debug.Assert(typeof(T).IsInSubtypeRelationshipWith(sourceConverter.Type!));
 028            Debug.Assert(sourceConverter.SourceConverterForCastingConverter is null, "casting converters should not be l
 29
 030            _sourceConverter = sourceConverter;
 031            IsInternalConverter = sourceConverter.IsInternalConverter;
 032            IsInternalConverterForNumberType = sourceConverter.IsInternalConverterForNumberType;
 033            ConverterStrategy = sourceConverter.ConverterStrategy;
 034            CanBePolymorphic = sourceConverter.CanBePolymorphic;
 35
 36            // Ensure HandleNull values reflect the exact configuration of the source converter
 037            HandleNullOnRead = sourceConverter.HandleNullOnRead;
 038            HandleNullOnWrite = sourceConverter.HandleNullOnWrite;
 039            HandleNull = sourceConverter.HandleNullOnWrite;
 040        }
 41
 042        internal override JsonConverter? SourceConverterForCastingConverter => _sourceConverter;
 43
 44        public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 045            => JsonSerializer.UnboxOnRead<T>(_sourceConverter.ReadAsObject(ref reader, typeToConvert, options));
 46
 47        public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
 048            => _sourceConverter.WriteAsObject(writer, value, options);
 49
 50        internal override bool OnTryRead(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options, s
 051        {
 052            bool result = _sourceConverter.OnTryReadAsObject(ref reader, typeToConvert, options, ref state, out object? 
 053            value = JsonSerializer.UnboxOnRead<T>(sourceValue);
 054            return result;
 055        }
 56
 57        internal override bool OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, ref WriteStack 
 058            => _sourceConverter.OnTryWriteAsObject(writer, value, options, ref state);
 59
 60        public override T ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions option
 061        {
 062            if (_sourceConverter.Type == typeof(T))
 063            {
 064                return JsonSerializer.UnboxOnRead<T>(_sourceConverter.ReadAsPropertyNameAsObject(ref reader, typeToConve
 65            }
 66            else
 067            {
 68                // The source converter's Type doesn't match T. Using the source converter's
 69                // ReadAsPropertyName could fail or behave incorrectly: the source's fallback
 70                // would look up a converter for its type (e.g. object), not T. Use our base
 71                // class logic instead, which correctly uses the fallback for type T.
 072                return base.ReadAsPropertyName(ref reader, typeToConvert, options);
 73            }
 074        }
 75
 76        internal override T ReadAsPropertyNameCore(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions 
 077        {
 078            if (_sourceConverter.Type == typeof(T))
 079            {
 080                return JsonSerializer.UnboxOnRead<T>(_sourceConverter.ReadAsPropertyNameCoreAsObject(ref reader, typeToC
 81            }
 82            else
 083            {
 84                // The source converter's Type doesn't match T. Using the source converter's
 85                // ReadAsPropertyNameCore could fail or behave incorrectly: the source's fallback
 86                // would look up a converter for its type (e.g. object), not T. Use our base
 87                // class logic instead, which correctly uses the fallback for type T.
 088                return base.ReadAsPropertyNameCore(ref reader, typeToConvert, options);
 89            }
 090        }
 91
 92        public override void WriteAsPropertyName(Utf8JsonWriter writer, [DisallowNull] T value, JsonSerializerOptions op
 093        {
 094            if (_sourceConverter.Type == typeof(T))
 095            {
 096                _sourceConverter.WriteAsPropertyNameAsObject(writer, value, options);
 097            }
 98            else
 099            {
 100                // The source converter's Type doesn't match T. Using the source converter's
 101                // WriteAsPropertyName could cause infinite recursion: the source's fallback
 102                // would look up a converter for its type (e.g. object), which might delegate
 103                // back to us via the runtime type lookup. Use our base class logic instead,
 104                // which correctly uses the fallback for type T.
 0105                base.WriteAsPropertyName(writer, value, options);
 0106            }
 0107        }
 108
 109        internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, T value, JsonSerializerOptions options, bo
 0110        {
 0111            if (_sourceConverter.Type == typeof(T))
 0112            {
 0113                _sourceConverter.WriteAsPropertyNameCoreAsObject(writer, value, options, isWritingExtensionDataProperty)
 0114            }
 115            else
 0116            {
 117                // The source converter's Type doesn't match T. Using the source converter's
 118                // WriteAsPropertyNameCore could cause infinite recursion: the source's fallback
 119                // would look up a converter for its type (e.g. object), which might delegate
 120                // back to us via the runtime type lookup. Use our base class logic instead,
 121                // which correctly uses the fallback for type T.
 0122                base.WriteAsPropertyNameCore(writer, value!, options, isWritingExtensionDataProperty);
 0123            }
 0124        }
 125
 126        internal override T ReadNumberWithCustomHandling(ref Utf8JsonReader reader, JsonNumberHandling handling, JsonSer
 0127            => JsonSerializer.UnboxOnRead<T>(_sourceConverter.ReadNumberWithCustomHandlingAsObject(ref reader, handling,
 128
 129        internal override void WriteNumberWithCustomHandling(Utf8JsonWriter writer, T? value, JsonNumberHandling handlin
 0130            => _sourceConverter.WriteNumberWithCustomHandlingAsObject(writer, value, handling);
 131
 132        internal override JsonSchema? GetSchema(JsonNumberHandling numberHandling)
 0133            => _sourceConverter.GetSchema(numberHandling);
 134    }
 135}