Repository navigation
Expand file tree
/
Copy pathJsonConverterFactory.cs
More file actions
162 lines (130 loc) · 5.84 KB
/
Copy pathJsonConverterFactory.cs
File metadata and controls
162 lines (130 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Text.Json.Serialization.Metadata;
namespace System.Text.Json.Serialization
{
/// <summary>
/// Supports converting several types by using a factory pattern.
/// </summary>
/// <remarks>
/// This is useful for converters supporting generics, such as a converter for <see cref="System.Collections.Generic.List{T}"/>.
/// </remarks>
public abstract class JsonConverterFactory : JsonConverter
{
/// <summary>
/// When overridden, constructs a new <see cref="JsonConverterFactory"/> instance.
/// </summary>
protected JsonConverterFactory() { }
private protected override ConverterStrategy GetDefaultConverterStrategy() => ConverterStrategy.None;
/// <summary>
/// Create a converter for the provided <see cref="System.Type"/>.
/// </summary>
/// <param name="typeToConvert">The <see cref="System.Type"/> being converted.</param>
/// <param name="options">The <see cref="JsonSerializerOptions"/> being used.</param>
/// <returns>
/// An instance of a <see cref="JsonConverter{T}"/> where T is compatible with <paramref name="typeToConvert"/>.
/// If <see langword="null"/> is returned, a <see cref="NotSupportedException"/> will be thrown.
/// </returns>
public abstract JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options);
internal JsonConverter GetConverterInternal(Type typeToConvert, JsonSerializerOptions options)
{
Debug.Assert(CanConvert(typeToConvert));
JsonConverter? converter = CreateConverter(typeToConvert, options);
switch (converter)
{
case null:
ThrowHelper.ThrowInvalidOperationException_SerializerConverterFactoryReturnsNull(GetType());
break;
case JsonConverterFactory:
ThrowHelper.ThrowInvalidOperationException_SerializerConverterFactoryReturnsJsonConverterFactorty(GetType());
break;
}
return converter;
}
internal sealed override object? ReadAsObject(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
Debug.Fail("We should never get here.");
throw new InvalidOperationException();
}
internal sealed override bool OnTryReadAsObject(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options,
scoped ref ReadStack state,
out object? value)
{
Debug.Fail("We should never get here.");
throw new InvalidOperationException();
}
internal sealed override bool TryReadAsObject(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options,
scoped ref ReadStack state,
out object? value)
{
Debug.Fail("We should never get here.");
throw new InvalidOperationException();
}
internal sealed override object? ReadAsPropertyNameAsObject(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
Debug.Fail("We should never get here.");
throw new InvalidOperationException();
}
internal sealed override object? ReadAsPropertyNameCoreAsObject(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
Debug.Fail("We should never get here.");
throw new InvalidOperationException();
}
internal sealed override object? ReadNumberWithCustomHandlingAsObject(ref Utf8JsonReader reader, JsonNumberHandling handling, JsonSerializerOptions options)
{
Debug.Fail("We should never get here.");
throw new InvalidOperationException();
}
internal sealed override void WriteAsObject(Utf8JsonWriter writer, object? value, JsonSerializerOptions options)
{
Debug.Fail("We should never get here.");
throw new InvalidOperationException();
}
internal sealed override bool OnTryWriteAsObject(
Utf8JsonWriter writer,
object? value,
JsonSerializerOptions options,
ref WriteStack state)
{
Debug.Fail("We should never get here.");
throw new InvalidOperationException();
}
internal sealed override bool TryWriteAsObject(
Utf8JsonWriter writer,
object? value,
JsonSerializerOptions options,
ref WriteStack state)
{
Debug.Fail("We should never get here.");
throw new InvalidOperationException();
}
internal sealed override void WriteAsPropertyNameAsObject(Utf8JsonWriter writer, object? value, JsonSerializerOptions options)
{
Debug.Fail("We should never get here.");
throw new InvalidOperationException();
}
/// <inheritdoc/>
public sealed override Type? Type => null;
internal sealed override void WriteAsPropertyNameCoreAsObject(
Utf8JsonWriter writer,
object? value,
JsonSerializerOptions options,
bool isWritingExtensionDataProperty)
{
Debug.Fail("We should never get here.");
throw new InvalidOperationException();
}
internal sealed override void WriteNumberWithCustomHandlingAsObject(Utf8JsonWriter writer, object? value, JsonNumberHandling handling)
{
Debug.Fail("We should never get here.");
throw new InvalidOperationException();
}
}
}