| | | 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 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.Text.Json.Serialization.Metadata; |
| | | 6 | | |
| | | 7 | | namespace 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> |
| | 834 | 20 | | protected JsonConverterFactory() { } |
| | | 21 | | |
| | 278 | 22 | | 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) |
| | 3915 | 36 | | { |
| | 3915 | 37 | | Debug.Assert(CanConvert(typeToConvert)); |
| | | 38 | | |
| | 3915 | 39 | | JsonConverter? converter = CreateConverter(typeToConvert, options); |
| | 3915 | 40 | | switch (converter) |
| | | 41 | | { |
| | | 42 | | case null: |
| | 0 | 43 | | ThrowHelper.ThrowInvalidOperationException_SerializerConverterFactoryReturnsNull(GetType()); |
| | | 44 | | break; |
| | | 45 | | case JsonConverterFactory: |
| | 0 | 46 | | ThrowHelper.ThrowInvalidOperationException_SerializerConverterFactoryReturnsJsonConverterFactory(Get |
| | | 47 | | break; |
| | | 48 | | } |
| | | 49 | | |
| | 3915 | 50 | | return converter; |
| | 3915 | 51 | | } |
| | | 52 | | |
| | | 53 | | internal sealed override object? ReadAsObject(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptio |
| | 0 | 54 | | { |
| | 0 | 55 | | 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) |
| | 0 | 66 | | { |
| | 0 | 67 | | 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) |
| | 0 | 78 | | { |
| | 0 | 79 | | 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 |
| | 0 | 85 | | { |
| | 0 | 86 | | 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 |
| | 0 | 92 | | { |
| | 0 | 93 | | Debug.Fail("We should never get here."); |
| | | 94 | | |
| | | 95 | | throw new InvalidOperationException(); |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | internal sealed override object? ReadNumberWithCustomHandlingAsObject(ref Utf8JsonReader reader, JsonNumberHandl |
| | 0 | 99 | | { |
| | 0 | 100 | | 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) |
| | 0 | 106 | | { |
| | 0 | 107 | | 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) |
| | 0 | 117 | | { |
| | 0 | 118 | | 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) |
| | 0 | 128 | | { |
| | 0 | 129 | | 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 |
| | 0 | 135 | | { |
| | 0 | 136 | | Debug.Fail("We should never get here."); |
| | | 137 | | |
| | | 138 | | throw new InvalidOperationException(); |
| | | 139 | | } |
| | | 140 | | |
| | | 141 | | /// <inheritdoc/> |
| | 0 | 142 | | 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) |
| | 0 | 149 | | { |
| | 0 | 150 | | 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 |
| | 0 | 156 | | { |
| | 0 | 157 | | Debug.Fail("We should never get here."); |
| | | 158 | | |
| | | 159 | | throw new InvalidOperationException(); |
| | | 160 | | } |
| | | 161 | | } |
| | | 162 | | } |