< Summary

Information
Class: System.Text.Json.Serialization.Converters.FSharpValueOptionConverter<T1, T2>
Assembly: System.Text.Json
File(s): C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\Converters\FSharp\FSharpValueOptionConverter.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 49
Coverable lines: 49
Total lines: 96
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 12
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%110%
OnTryRead(...)0%660%
OnTryWrite(...)0%220%
Write(...)0%220%
Read(...)0%220%

File(s)

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\Converters\FSharp\FSharpValueOptionConverter.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Diagnostics.CodeAnalysis;
 5using System.Text.Json.Serialization.Metadata;
 6
 7namespace System.Text.Json.Serialization.Converters
 8{
 9    // Converter for F# struct optional values: https://fsharp.github.io/fsharp-core-docs/reference/fsharp-core-fsharpva
 10    // Serializes `ValueSome(value)` using the format of `value` and `ValueNone` values as `null`.
 11    internal sealed class FSharpValueOptionConverter<TValueOption, TElement> : JsonConverter<TValueOption>
 12        where TValueOption : struct, IEquatable<TValueOption>
 13    {
 014        internal override Type? ElementType => typeof(TElement);
 015        internal override JsonConverter? NullableElementConverter => _elementConverter;
 16        // 'ValueNone' is encoded using 'default' at runtime and serialized as 'null' in JSON.
 017        public override bool HandleNull => true;
 18
 19        private readonly JsonConverter<TElement> _elementConverter;
 20        private readonly FSharpCoreReflectionProxy.StructGetter<TValueOption, TElement> _optionValueGetter;
 21        private readonly Func<TElement?, TValueOption> _optionConstructor;
 22
 23        [RequiresUnreferencedCode(FSharpCoreReflectionProxy.FSharpCoreUnreferencedCodeMessage)]
 24        [RequiresDynamicCode(FSharpCoreReflectionProxy.FSharpCoreUnreferencedCodeMessage)]
 025        public FSharpValueOptionConverter(JsonConverter<TElement> elementConverter)
 026        {
 027            _elementConverter = elementConverter;
 028            _optionValueGetter = FSharpCoreReflectionProxy.Instance.CreateFSharpValueOptionValueGetter<TValueOption, TEl
 029            _optionConstructor = FSharpCoreReflectionProxy.Instance.CreateFSharpValueOptionSomeConstructor<TValueOption,
 030            ConverterStrategy = elementConverter.ConverterStrategy;
 031        }
 32
 33        internal override bool OnTryRead(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options, s
 034        {
 35            // `null` values deserialize as `ValueNone`
 036            if (!state.IsContinuation && reader.TokenType == JsonTokenType.Null)
 037            {
 038                value = default;
 039                return true;
 40            }
 41
 042            state.Current.JsonPropertyInfo = state.Current.JsonTypeInfo.ElementTypeInfo!.PropertyInfoForTypeInfo;
 043            if (_elementConverter.TryRead(ref reader, typeof(TElement), options, ref state, out TElement? element, out _
 044            {
 045                value = _optionConstructor(element);
 046                return true;
 47            }
 48
 049            value = default;
 050            return false;
 051        }
 52
 53        internal override bool OnTryWrite(Utf8JsonWriter writer, TValueOption value, JsonSerializerOptions options, ref 
 054        {
 055            if (value.Equals(default))
 056            {
 57                // Write `ValueNone` values as null
 058                writer.WriteNullValue();
 059                return true;
 60            }
 61
 062            TElement element = _optionValueGetter(ref value);
 63
 064            state.Current.JsonPropertyInfo = state.Current.JsonTypeInfo.ElementTypeInfo!.PropertyInfoForTypeInfo;
 065            return _elementConverter.TryWrite(writer, element, options, ref state);
 066        }
 67
 68        // Since this is a hybrid converter (ConverterStrategy depends on the element converter),
 69        // we need to override the value converter Write and Read methods too.
 70
 71        public override void Write(Utf8JsonWriter writer, TValueOption value, JsonSerializerOptions options)
 072        {
 073            if (value.Equals(default))
 074            {
 75                // Write `ValueNone` values as null
 076                writer.WriteNullValue();
 077            }
 78            else
 079            {
 080                TElement element = _optionValueGetter(ref value);
 081                _elementConverter.Write(writer, element, options);
 082            }
 083        }
 84
 85        public override TValueOption Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 086        {
 087            if (reader.TokenType == JsonTokenType.Null)
 088            {
 089                return default;
 90            }
 91
 092            TElement? element = _elementConverter.Read(ref reader, typeToConvert, options);
 093            return _optionConstructor(element);
 094        }
 95    }
 96}