< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 24
Coverable lines: 24
Total lines: 51
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 8
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
Add(TElement& modreq(...)0%220%
CreateCollection(...)0%220%
ConfigureJsonTypeInfo(...)0%440%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/IListOfTConverter.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    /// <summary>
 11    /// Converter for <cref>System.Collections.Generic.IList{TElement}</cref>.
 12    /// </summary>
 13    internal sealed class IListOfTConverter<TCollection, TElement>
 14        : IEnumerableDefaultConverter<TCollection, TElement>
 15        where TCollection : IList<TElement>
 16    {
 017        internal override bool CanPopulate => true;
 18
 19        protected override void Add(in TElement value, ref ReadStack state)
 020        {
 021            TCollection collection = (TCollection)state.Current.ReturnValue!;
 022            collection.Add(value);
 023            if (IsValueType)
 024            {
 025                state.Current.ReturnValue = collection;
 026            };
 027        }
 28
 29        protected override void CreateCollection(ref Utf8JsonReader reader, scoped ref ReadStack state, JsonSerializerOp
 030        {
 031            base.CreateCollection(ref reader, ref state, options);
 032            TCollection returnValue = (TCollection)state.Current.ReturnValue!;
 033            if (returnValue.IsReadOnly)
 034            {
 035                state.Current.ReturnValue = null; // clear out for more accurate JsonPath reporting.
 036                ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(Type, ref reader, ref state);
 37            }
 038        }
 39
 40        internal override void ConfigureJsonTypeInfo(JsonTypeInfo jsonTypeInfo, JsonSerializerOptions options)
 041        {
 42            // Deserialize as List<T> for interface types that support it.
 043            if (jsonTypeInfo.CreateObject is null && Type.IsAssignableFrom(typeof(List<TElement>)))
 044            {
 045                Debug.Assert(Type.IsInterface);
 046                jsonTypeInfo.CreateObject = () => new List<TElement>();
 047            }
 048        }
 49    }
 50}
 51