OptionalConverter<T>
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(new byte[] {
0,
0,
1
})]
public sealed class OptionalConverter<[System.Runtime.CompilerServices.Nullable(2)] [DynamicallyAccessedMembers((DynamicallyAccessedMemberTypes)8739)] T> : JsonConverter<Optional<T>>
{
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = "Public properties/fields are preserved")]
public override void Write(Utf8JsonWriter writer, [System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] Optional<T> value, JsonSerializerOptions options)
{
if (value.IsUndefined)
throw new InvalidOperationException(ExceptionMessages.UndefinedValueDetected);
if (value.IsNull)
writer.WriteNullValue();
else
JsonSerializer.Serialize<T>(writer, value.OrDefault(), options);
}
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = "Public properties/fields are preserved")]
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
public override Optional<T> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new Optional<T>((reader.TokenType == JsonTokenType.Null) ? default(T) : JsonSerializer.Deserialize<T>(ref reader, options));
}
}
}