<PackageReference Include="System.Text.Json" Version="5.0.0-preview.7.20364.11" />
KeyValuePairConverterFactory
using System.
Collections.
Generic;
using System.
Diagnostics.
CodeAnalysis;
using System.
Reflection;
namespace System.
Text.
Json.
Serialization.
Converters
{
internal sealed class KeyValuePairConverterFactory :
JsonConverterFactory
{
public override bool CanConvert(
Type typeToConvert)
{
if (!
typeToConvert.
IsGenericType)
return false;
Type genericTypeDefinition =
typeToConvert.
GetGenericTypeDefinition();
return genericTypeDefinition ==
typeof(
KeyValuePair<
,
>);
}
[
System.
Diagnostics.
CodeAnalysis.
DynamicDependency(
"#ctor()",
typeof(
KeyValuePairConverter<
,
>))]
public override JsonConverter CreateConverter(
Type type,
JsonSerializerOptions options)
{
Type type2 =
type.
GetGenericArguments()[
0];
Type type3 =
type.
GetGenericArguments()[
1];
JsonConverter jsonConverter = (
JsonConverter)
Activator.
CreateInstance(
typeof(
KeyValuePairConverter<
,
>).
MakeGenericType(
type2,
type3),
BindingFlags.
Instance |
BindingFlags.
Public,
null,
null,
null);
jsonConverter.
Initialize(
options);
return jsonConverter;
}
}
}