< Summary

Information
Class: System.Text.Json.Serialization.Converters.FSharpOptionConverter<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\FSharpOptionConverter.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 49
Coverable lines: 49
Total lines: 94
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\FSharpOptionConverter.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# optional values: https://fsharp.github.io/fsharp-core-docs/reference/fsharp-core-option-1.html
 10    // Serializes `Some(value)` using the format of `value` and `None` values as `null`.
 11    internal sealed class FSharpOptionConverter<TOption, TElement> : JsonConverter<TOption>
 12        where TOption : class
 13    {
 014        internal override Type? ElementType => typeof(TElement);
 015        internal override JsonConverter? NullableElementConverter => _elementConverter;
 16        // 'None' is encoded using 'null' at runtime and serialized as 'null' in JSON.
 017        public override bool HandleNull => true;
 18
 19        private readonly JsonConverter<TElement> _elementConverter;
 20        private readonly Func<TOption, TElement> _optionValueGetter;
 21        private readonly Func<TElement?, TOption> _optionConstructor;
 22
 23        [RequiresUnreferencedCode(FSharpCoreReflectionProxy.FSharpCoreUnreferencedCodeMessage)]
 24        [RequiresDynamicCode(FSharpCoreReflectionProxy.FSharpCoreUnreferencedCodeMessage)]
 025        public FSharpOptionConverter(JsonConverter<TElement> elementConverter)
 026        {
 027            _elementConverter = elementConverter;
 028            _optionValueGetter = FSharpCoreReflectionProxy.Instance.CreateFSharpOptionValueGetter<TOption, TElement>();
 029            _optionConstructor = FSharpCoreReflectionProxy.Instance.CreateFSharpOptionSomeConstructor<TOption, TElement>
 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 `None`
 036            if (!state.IsContinuation && reader.TokenType == JsonTokenType.Null)
 037            {
 038                value = null;
 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 = null;
 050            return false;
 051        }
 52
 53        internal override bool OnTryWrite(Utf8JsonWriter writer, TOption value, JsonSerializerOptions options, ref Write
 054        {
 055            if (value is null)
 056            {
 57                // Write `None` values as null
 058                writer.WriteNullValue();
 059                return true;
 60            }
 61
 062            TElement element = _optionValueGetter(value);
 063            state.Current.JsonPropertyInfo = state.Current.JsonTypeInfo.ElementTypeInfo!.PropertyInfoForTypeInfo;
 064            return _elementConverter.TryWrite(writer, element, options, ref state);
 065        }
 66
 67        // Since this is a hybrid converter (ConverterStrategy depends on the element converter),
 68        // we need to override the value converter Write and Read methods too.
 69
 70        public override void Write(Utf8JsonWriter writer, TOption value, JsonSerializerOptions options)
 071        {
 072            if (value is null)
 073            {
 074                writer.WriteNullValue();
 075            }
 76            else
 077            {
 078                TElement element = _optionValueGetter(value);
 079                _elementConverter.Write(writer, element, options);
 080            }
 081        }
 82
 83        public override TOption? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 084        {
 085            if (reader.TokenType == JsonTokenType.Null)
 086            {
 087                return null;
 88            }
 89
 090            TElement? element = _elementConverter.Read(ref reader, typeToConvert, options);
 091            return _optionConstructor(element);
 092        }
 93    }
 94}