< Summary

Information
Line coverage
22%
Covered lines: 8
Uncovered lines: 27
Coverable lines: 35
Total lines: 162
Line coverage: 22.8%
Branch coverage
50%
Covered branches: 2
Total branches: 4
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonConverterFactory.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.Text.Json.Serialization.Metadata;
 6
 7namespace System.Text.Json.Serialization
 8{
 9    /// <summary>
 10    /// Supports converting several types by using a factory pattern.
 11    /// </summary>
 12    /// <remarks>
 13    /// This is useful for converters supporting generics, such as a converter for <see cref="System.Collections.Generic
 14    /// </remarks>
 15    public abstract class JsonConverterFactory : JsonConverter
 16    {
 17        /// <summary>
 18        /// When overridden, constructs a new <see cref="JsonConverterFactory"/> instance.
 19        /// </summary>
 83420        protected JsonConverterFactory() { }
 21
 27822        private protected override ConverterStrategy GetDefaultConverterStrategy() => ConverterStrategy.None;
 23
 24        /// <summary>
 25        /// Create a converter for the provided <see cref="System.Type"/>.
 26        /// </summary>
 27        /// <param name="typeToConvert">The <see cref="System.Type"/> being converted.</param>
 28        /// <param name="options">The <see cref="JsonSerializerOptions"/> being used.</param>
 29        /// <returns>
 30        /// An instance of a <see cref="JsonConverter{T}"/> where T is compatible with <paramref name="typeToConvert"/>.
 31        /// If <see langword="null"/> is returned, a <see cref="NotSupportedException"/> will be thrown.
 32        /// </returns>
 33        public abstract JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options);
 34
 35        internal JsonConverter GetConverterInternal(Type typeToConvert, JsonSerializerOptions options)
 391536        {
 391537            Debug.Assert(CanConvert(typeToConvert));
 38
 391539            JsonConverter? converter = CreateConverter(typeToConvert, options);
 391540            switch (converter)
 41            {
 42                case null:
 043                    ThrowHelper.ThrowInvalidOperationException_SerializerConverterFactoryReturnsNull(GetType());
 44                    break;
 45                case JsonConverterFactory:
 046                    ThrowHelper.ThrowInvalidOperationException_SerializerConverterFactoryReturnsJsonConverterFactory(Get
 47                    break;
 48            }
 49
 391550            return converter;
 391551        }
 52
 53        internal sealed override object? ReadAsObject(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptio
 054        {
 055            Debug.Fail("We should never get here.");
 56
 57            throw new InvalidOperationException();
 58        }
 59
 60        internal sealed override bool OnTryReadAsObject(
 61            ref Utf8JsonReader reader,
 62            Type typeToConvert,
 63            JsonSerializerOptions options,
 64            scoped ref ReadStack state,
 65            out object? value)
 066        {
 067            Debug.Fail("We should never get here.");
 68
 69            throw new InvalidOperationException();
 70        }
 71
 72        internal sealed override bool TryReadAsObject(
 73            ref Utf8JsonReader reader,
 74            Type typeToConvert,
 75            JsonSerializerOptions options,
 76            scoped ref ReadStack state,
 77            out object? value)
 078        {
 079            Debug.Fail("We should never get here.");
 80
 81            throw new InvalidOperationException();
 82        }
 83
 84        internal sealed override object? ReadAsPropertyNameAsObject(ref Utf8JsonReader reader, Type typeToConvert, JsonS
 085        {
 086            Debug.Fail("We should never get here.");
 87
 88            throw new InvalidOperationException();
 89        }
 90
 91        internal sealed override object? ReadAsPropertyNameCoreAsObject(ref Utf8JsonReader reader, Type typeToConvert, J
 092        {
 093            Debug.Fail("We should never get here.");
 94
 95            throw new InvalidOperationException();
 96        }
 97
 98        internal sealed override object? ReadNumberWithCustomHandlingAsObject(ref Utf8JsonReader reader, JsonNumberHandl
 099        {
 0100            Debug.Fail("We should never get here.");
 101
 102            throw new InvalidOperationException();
 103        }
 104
 105        internal sealed override void WriteAsObject(Utf8JsonWriter writer, object? value, JsonSerializerOptions options)
 0106        {
 0107            Debug.Fail("We should never get here.");
 108
 109            throw new InvalidOperationException();
 110        }
 111
 112        internal sealed override bool OnTryWriteAsObject(
 113            Utf8JsonWriter writer,
 114            object? value,
 115            JsonSerializerOptions options,
 116            ref WriteStack state)
 0117        {
 0118            Debug.Fail("We should never get here.");
 119
 120            throw new InvalidOperationException();
 121        }
 122
 123        internal sealed override bool TryWriteAsObject(
 124            Utf8JsonWriter writer,
 125            object? value,
 126            JsonSerializerOptions options,
 127            ref WriteStack state)
 0128        {
 0129            Debug.Fail("We should never get here.");
 130
 131            throw new InvalidOperationException();
 132        }
 133
 134        internal sealed override void WriteAsPropertyNameAsObject(Utf8JsonWriter writer, object? value, JsonSerializerOp
 0135        {
 0136            Debug.Fail("We should never get here.");
 137
 138            throw new InvalidOperationException();
 139        }
 140
 141        /// <inheritdoc/>
 0142        public sealed override Type? Type => null;
 143
 144        internal sealed override void WriteAsPropertyNameCoreAsObject(
 145            Utf8JsonWriter writer,
 146            object? value,
 147            JsonSerializerOptions options,
 148            bool isWritingExtensionDataProperty)
 0149        {
 0150            Debug.Fail("We should never get here.");
 151
 152            throw new InvalidOperationException();
 153        }
 154
 155        internal sealed override void WriteNumberWithCustomHandlingAsObject(Utf8JsonWriter writer, object? value, JsonNu
 0156        {
 0157            Debug.Fail("We should never get here.");
 158
 159            throw new InvalidOperationException();
 160        }
 161    }
 162}

Methods/Properties

.ctor()
GetDefaultConverterStrategy()
GetConverterInternal(System.Type,System.Text.Json.JsonSerializerOptions)
ReadAsObject(System.Text.Json.Utf8JsonReader&,System.Type,System.Text.Json.JsonSerializerOptions)
OnTryReadAsObject(System.Text.Json.Utf8JsonReader&,System.Type,System.Text.Json.JsonSerializerOptions,System.Text.Json.ReadStack&,System.Object&)
TryReadAsObject(System.Text.Json.Utf8JsonReader&,System.Type,System.Text.Json.JsonSerializerOptions,System.Text.Json.ReadStack&,System.Object&)
ReadAsPropertyNameAsObject(System.Text.Json.Utf8JsonReader&,System.Type,System.Text.Json.JsonSerializerOptions)
ReadAsPropertyNameCoreAsObject(System.Text.Json.Utf8JsonReader&,System.Type,System.Text.Json.JsonSerializerOptions)
ReadNumberWithCustomHandlingAsObject(System.Text.Json.Utf8JsonReader&,System.Text.Json.Serialization.JsonNumberHandling,System.Text.Json.JsonSerializerOptions)
WriteAsObject(System.Text.Json.Utf8JsonWriter,System.Object,System.Text.Json.JsonSerializerOptions)
OnTryWriteAsObject(System.Text.Json.Utf8JsonWriter,System.Object,System.Text.Json.JsonSerializerOptions,System.Text.Json.WriteStack&)
TryWriteAsObject(System.Text.Json.Utf8JsonWriter,System.Object,System.Text.Json.JsonSerializerOptions,System.Text.Json.WriteStack&)
WriteAsPropertyNameAsObject(System.Text.Json.Utf8JsonWriter,System.Object,System.Text.Json.JsonSerializerOptions)
Type()
WriteAsPropertyNameCoreAsObject(System.Text.Json.Utf8JsonWriter,System.Object,System.Text.Json.JsonSerializerOptions,System.Boolean)
WriteNumberWithCustomHandlingAsObject(System.Text.Json.Utf8JsonWriter,System.Object,System.Text.Json.Serialization.JsonNumberHandling)