DotNext by .NET Foundation and Contributors

<PackageReference Include="DotNext" Version="4.2.0-beta.1" />

 OptionalConverterFactory

Represents JSON converter for Optional<T> data type.
using System; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Text.Json; using System.Text.Json.Serialization; namespace DotNext.Text.Json { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public sealed class OptionalConverterFactory : JsonConverterFactory { public override bool CanConvert(Type typeToConvert) { return typeToConvert.IsOptional(); } [DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(OptionalConverter<>))] [return: System.Runtime.CompilerServices.Nullable(2)] public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) { Type underlyingType = Optional.GetUnderlyingType(typeToConvert); if ((object)underlyingType != null) return Activator.CreateInstance(typeof(OptionalConverter<>).MakeGenericType(underlyingType)) as JsonConverter; return null; } } }