< Summary

Information
Class: System.Text.Json.Serialization.Converters.ListOfTConverter<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\ListOfTConverter.cs
Line coverage
19%
Covered lines: 8
Uncovered lines: 33
Coverable lines: 41
Total lines: 76
Line coverage: 19.5%
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%11100%
CreateCollection(...)50%6655.55%
OnWriteResume(...)0%12120%

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\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)
 1017        {
 1018            ((TCollection)state.Current.ReturnValue!).Add(value);
 1019        }
 20
 21        protected override void CreateCollection(ref Utf8JsonReader reader, scoped ref ReadStack state, JsonSerializerOp
 21222        {
 21223            if (state.ParentProperty?.TryGetPrePopulatedValue(ref state) == true)
 024            {
 025                return;
 26            }
 27
 21228            if (state.Current.JsonTypeInfo.CreateObject == null)
 029            {
 030                ThrowHelper.ThrowNotSupportedException_SerializationNotSupported(state.Current.JsonTypeInfo.Type);
 31            }
 32
 21233            state.Current.ReturnValue = state.Current.JsonTypeInfo.CreateObject();
 21234        }
 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 == 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}