< Summary

Information
Class: System.Text.Json.Serialization.Converters.IReadOnlySetOfTConverter<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\Collection\IReadOnlySetOfTConverter.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 22
Coverable lines: 22
Total lines: 47
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 10
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(TElement& modreq(...)0%220%
CreateCollection(...)0%220%
ConfigureJsonTypeInfo(...)0%660%

File(s)

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\Converters\Collection\IReadOnlySetOfTConverter.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.Text.Json.Serialization.Metadata;
 7
 8namespace System.Text.Json.Serialization.Converters
 9{
 10    internal sealed class IReadOnlySetOfTConverter<TCollection, TElement>
 11        : IEnumerableDefaultConverter<TCollection, TElement>
 12        where TCollection : IReadOnlySet<TElement>
 13    {
 014        private readonly bool _isDeserializable = typeof(TCollection).IsAssignableFrom(typeof(HashSet<TElement>));
 15
 16        protected override void Add(in TElement value, ref ReadStack state)
 017        {
 18            // Directly convert to HashSet<TElement> since IReadOnlySet<T> does not have an Add method.
 019            HashSet<TElement> collection = (HashSet<TElement>)state.Current.ReturnValue!;
 020            collection.Add(value);
 021            if (IsValueType)
 022            {
 023                state.Current.ReturnValue = collection;
 024            }
 025        }
 26
 27        protected override void CreateCollection(ref Utf8JsonReader reader, scoped ref ReadStack state, JsonSerializerOp
 028        {
 029            if (!_isDeserializable)
 030            {
 031                ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(Type, ref reader, ref state);
 32            }
 33
 034            state.Current.ReturnValue = new HashSet<TElement>();
 035        }
 36
 37        internal override void ConfigureJsonTypeInfo(JsonTypeInfo jsonTypeInfo, JsonSerializerOptions options)
 038        {
 39            // Deserialize as HashSet<TElement> for interface types that support it.
 040            if (jsonTypeInfo.CreateObject is null && Type.IsAssignableFrom(typeof(HashSet<TElement>)))
 041            {
 042                Debug.Assert(Type.IsInterface);
 043                jsonTypeInfo.CreateObject = () => new HashSet<TElement>();
 044            }
 045        }
 46    }
 47}