< Summary

Information
Line coverage
0%
Covered lines: 0
Uncovered lines: 52
Coverable lines: 52
Total lines: 89
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 22
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(System.Object& modreq(...)0%220%
CreateCollection(...)0%220%
OnWriteResume(...)0%12120%
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\IListConverter.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;
 5using System.Collections.Generic;
 6using System.Diagnostics;
 7using System.Text.Json.Serialization.Metadata;
 8
 9namespace System.Text.Json.Serialization.Converters
 10{
 11    /// Converter for <cref>System.Collections.IList</cref>.
 12    internal sealed class IListConverter<TCollection>
 13        : JsonCollectionConverter<TCollection, object?>
 14        where TCollection : IList
 15    {
 016        internal override bool CanPopulate => true;
 17
 18        protected override void Add(in object? value, ref ReadStack state)
 019        {
 020            TCollection collection = (TCollection)state.Current.ReturnValue!;
 021            collection.Add(value);
 022            if (IsValueType)
 023            {
 024                state.Current.ReturnValue = collection;
 025            }
 026        }
 27
 28        protected override void CreateCollection(ref Utf8JsonReader reader, scoped ref ReadStack state, JsonSerializerOp
 029        {
 030            base.CreateCollection(ref reader, ref state, options);
 031            TCollection returnValue = (TCollection)state.Current.ReturnValue!;
 032            if (returnValue.IsReadOnly)
 033            {
 034                state.Current.ReturnValue = null; // clear out for more accurate JsonPath reporting.
 035                ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(Type, ref reader, ref state);
 36            }
 037        }
 38
 39        protected override bool OnWriteResume(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, r
 040        {
 041            IList list = value;
 42
 43            // Using an index is 2x faster than using an enumerator.
 044            int index = state.Current.EnumeratorIndex;
 045            JsonConverter<object?> elementConverter = GetElementConverter(ref state);
 46
 047            if (elementConverter.CanUseDirectReadOrWrite && state.Current.NumberHandling == null)
 048            {
 49                // Fast path that avoids validation and extra indirection.
 050                for (; index < list.Count; index++)
 051                {
 052                    elementConverter.Write(writer, list[index], options);
 053                }
 054            }
 55            else
 056            {
 057                for (; index < list.Count; index++)
 058                {
 059                    object? element = list[index];
 060                    if (!elementConverter.TryWrite(writer, element, options, ref state))
 061                    {
 062                        state.Current.EnumeratorIndex = index;
 063                        return false;
 64                    }
 65
 066                    state.Current.EndCollectionElement();
 67
 068                    if (ShouldFlush(ref state, writer))
 069                    {
 070                        state.Current.EnumeratorIndex = ++index;
 071                        return false;
 72                    }
 073                }
 074            }
 75
 076            return true;
 077        }
 78
 79        internal override void ConfigureJsonTypeInfo(JsonTypeInfo jsonTypeInfo, JsonSerializerOptions options)
 080        {
 81            // Deserialize as List<object?> for interface types that support it.
 082            if (jsonTypeInfo.CreateObject is null && Type.IsAssignableFrom(typeof(List<object?>)))
 083            {
 084                Debug.Assert(Type.IsInterface);
 085                jsonTypeInfo.CreateObject = () => new List<object?>();
 086            }
 087        }
 88    }
 89}