< Summary

Information
Class: System.Text.Json.Serialization.Converters.FSharpMapConverter<T1, T2, T3>
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\FSharpMapConverter.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 26
Coverable lines: 26
Total lines: 57
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
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%
Add(TKey,TValue& modreq(...)100%110%
CreateCollection(...)100%110%
ConvertCollection(...)0%440%

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\FSharpMapConverter.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.Collections.Generic;
 5using System.Diagnostics;
 6using System.Diagnostics.CodeAnalysis;
 7using System.Text.Json.Serialization.Metadata;
 8
 9namespace System.Text.Json.Serialization.Converters
 10{
 11    // Converter for F# maps: https://fsharp.github.io/fsharp-core-docs/reference/fsharp-collections-fsharpmap-2.html
 12    internal sealed class FSharpMapConverter<TMap, TKey, TValue> : DictionaryDefaultConverter<TMap, TKey, TValue>
 13        where TMap : IEnumerable<KeyValuePair<TKey, TValue>>
 14        where TKey : notnull
 15    {
 16        private readonly Func<IEnumerable<Tuple<TKey, TValue>>, TMap> _mapConstructor;
 17
 18        [RequiresUnreferencedCode(FSharpCoreReflectionProxy.FSharpCoreUnreferencedCodeMessage)]
 19        [RequiresDynamicCode(FSharpCoreReflectionProxy.FSharpCoreUnreferencedCodeMessage)]
 020        public FSharpMapConverter()
 021        {
 022            _mapConstructor = FSharpCoreReflectionProxy.Instance.CreateFSharpMapConstructor<TMap, TKey, TValue>();
 023        }
 24
 25        protected override void Add(TKey key, in TValue value, JsonSerializerOptions options, ref ReadStack state)
 026        {
 027            ((List<Tuple<TKey, TValue>>)state.Current.ReturnValue!).Add(new Tuple<TKey, TValue>(key, value));
 028        }
 29
 030        internal override bool CanHaveMetadata => false;
 31
 032        internal override bool SupportsCreateObjectDelegate => false;
 33        protected override void CreateCollection(ref Utf8JsonReader reader, scoped ref ReadStack state)
 034        {
 035            state.Current.ReturnValue = new List<Tuple<TKey, TValue>>();
 036        }
 37
 038        internal sealed override bool IsConvertibleCollection => true;
 39        protected override void ConvertCollection(ref ReadStack state, JsonSerializerOptions options)
 040        {
 041            List<Tuple<TKey, TValue>> listToConvert = (List<Tuple<TKey, TValue>>)state.Current.ReturnValue!;
 042            TMap map = _mapConstructor(listToConvert);
 043            state.Current.ReturnValue = map;
 44
 045            if (!options.AllowDuplicateProperties)
 046            {
 047                int totalItemsAdded = listToConvert.Count;
 048                int mapCount = ((ICollection<KeyValuePair<TKey, TValue>>)map).Count;
 49
 050                if (mapCount != totalItemsAdded)
 051                {
 052                    ThrowHelper.ThrowJsonException_DuplicatePropertyNotAllowed();
 53                }
 054            }
 055        }
 56    }
 57}