< Summary

Line coverage
12%
Covered lines: 5
Uncovered lines: 36
Coverable lines: 41
Total lines: 77
Line coverage: 12.1%
Branch coverage
16%
Covered branches: 3
Total branches: 18
Branch coverage: 16.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
Add(TElement& modreq(...)100%110%
CreateCollection(...)50%6655.55%
OnWriteResume(...)0%12120%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/ListOfTConverter.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;
 6
 7namespace System.Text.Json.Serialization.Converters
 8{
 9    /// Converter for <cref>System.Collections.Generic.List{TElement}</cref>.
 10    internal sealed class ListOfTConverter<TCollection, TElement>
 11        : IEnumerableDefaultConverter<TCollection, TElement>
 12        where TCollection : List<TElement>
 13    {
 014        internal override bool CanPopulate => true;
 15
 16        protected override void Add(in TElement value, ref ReadStack state)
 017        {
 018            ((TCollection)state.Current.ReturnValue!).Add(value);
 019        }
 20
 21        protected override void CreateCollection(ref Utf8JsonReader reader, scoped ref ReadStack state, JsonSerializerOp
 1422        {
 1423            if (state.ParentProperty?.TryGetPrePopulatedValue(ref state) == true)
 024            {
 025                return;
 26            }
 27
 1428            if (state.Current.JsonTypeInfo.CreateObject is null)
 029            {
 030                ThrowHelper.ThrowNotSupportedException_SerializationNotSupported(state.Current.JsonTypeInfo.Type);
 31            }
 32
 1433            state.Current.ReturnValue = state.Current.JsonTypeInfo.CreateObject();
 1434        }
 35
 36        protected override bool OnWriteResume(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, r
 037        {
 038            List<TElement> list = value;
 39
 40            // Using an index is 2x faster than using an enumerator.
 041            int index = state.Current.EnumeratorIndex;
 042            JsonConverter<TElement> elementConverter = GetElementConverter(ref state);
 43
 044            if (elementConverter.CanUseDirectReadOrWrite && state.Current.NumberHandling is null)
 045            {
 46                // Fast path that avoids validation and extra indirection.
 047                for (; index < list.Count; index++)
 048                {
 049                    elementConverter.Write(writer, list[index], options);
 050                }
 051            }
 52            else
 053            {
 054                for (; index < list.Count; index++)
 055                {
 056                    TElement element = list[index];
 057                    if (!elementConverter.TryWrite(writer, element, options, ref state))
 058                    {
 059                        state.Current.EnumeratorIndex = index;
 060                        return false;
 61                    }
 62
 063                    state.Current.EndCollectionElement();
 64
 065                    if (ShouldFlush(ref state, writer))
 066                    {
 067                        state.Current.EnumeratorIndex = ++index;
 068                        return false;
 69                    }
 070                }
 071            }
 72
 073            return true;
 074        }
 75    }
 76}
 77