FSharpValueOptionConverter<TValueOption, TElement>
sealed class FSharpValueOptionConverter<TValueOption, TElement> : JsonConverter<TValueOption> where TValueOption : struct, IEquatable<TValueOption>
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Text.Json.Serialization.Metadata;
namespace System.Text.Json.Serialization.Converters
{
internal sealed class FSharpValueOptionConverter<TValueOption, TElement> : JsonConverter<TValueOption> where TValueOption : struct, IEquatable<TValueOption>
{
private readonly JsonConverter<TElement> _elementConverter;
private readonly FSharpCoreReflectionProxy.StructGetter<TValueOption, TElement> _optionValueGetter;
private readonly Func<TElement, TValueOption> _optionConstructor;
internal override Type ElementType => typeof(TElement);
internal override JsonConverter NullableElementConverter => _elementConverter;
public override bool HandleNull => true;
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Uses Reflection to access FSharp.Core components at runtime.")]
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Uses Reflection to access FSharp.Core components at runtime.")]
public FSharpValueOptionConverter(JsonConverter<TElement> elementConverter)
{
_elementConverter = elementConverter;
_optionValueGetter = FSharpCoreReflectionProxy.Instance.CreateFSharpValueOptionValueGetter<TValueOption, TElement>();
_optionConstructor = FSharpCoreReflectionProxy.Instance.CreateFSharpValueOptionSomeConstructor<TValueOption, TElement>();
base.ConverterStrategy = elementConverter.ConverterStrategy;
}
internal override bool OnTryRead(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options, [System.Runtime.CompilerServices.ScopedRef] ref ReadStack state, out TValueOption value)
{
if (!state.IsContinuation && reader.TokenType == JsonTokenType.Null) {
value = default(TValueOption);
return true;
}
state.Current.JsonPropertyInfo = state.Current.JsonTypeInfo.ElementTypeInfo.PropertyInfoForTypeInfo;
if (_elementConverter.TryRead(ref reader, typeof(TElement), options, ref state, out TElement value2, out bool _)) {
value = _optionConstructor(value2);
return true;
}
value = default(TValueOption);
return false;
}
internal override bool OnTryWrite(Utf8JsonWriter writer, TValueOption value, JsonSerializerOptions options, ref WriteStack state)
{
if (value.Equals(default(TValueOption))) {
writer.WriteNullValue();
return true;
}
TElement value2 = _optionValueGetter(ref value);
state.Current.JsonPropertyInfo = state.Current.JsonTypeInfo.ElementTypeInfo.PropertyInfoForTypeInfo;
return _elementConverter.TryWrite(writer, ref value2, options, ref state);
}
public override void Write(Utf8JsonWriter writer, TValueOption value, JsonSerializerOptions options)
{
if (value.Equals(default(TValueOption)))
writer.WriteNullValue();
else {
TElement value2 = _optionValueGetter(ref value);
_elementConverter.Write(writer, value2, options);
}
}
public override TValueOption Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
return default(TValueOption);
TElement arg = _elementConverter.Read(ref reader, typeToConvert, options);
return _optionConstructor(arg);
}
}
}