< Summary

Information
Line coverage
0%
Covered lines: 0
Uncovered lines: 43
Coverable lines: 43
Total lines: 80
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 16
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(...)100%110%
CreateCollection(...)0%660%
OnWriteResume(...)0%10100%

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\StackOrQueueConverter.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.Diagnostics;
 6using System.Text.Json.Serialization.Metadata;
 7
 8namespace System.Text.Json.Serialization.Converters
 9{
 10    internal class StackOrQueueConverter<TCollection>
 11        : JsonCollectionConverter<TCollection, object?>
 12        where TCollection : IEnumerable
 13    {
 014        internal override bool CanPopulate => true;
 15
 16        protected sealed override void Add(in object? value, ref ReadStack state)
 017        {
 018            var addMethodDelegate = ((Action<TCollection, object?>?)state.Current.JsonTypeInfo.AddMethodDelegate);
 019            Debug.Assert(addMethodDelegate != null);
 020            addMethodDelegate((TCollection)state.Current.ReturnValue!, value);
 021        }
 22
 23        protected sealed override void CreateCollection(ref Utf8JsonReader reader, scoped ref ReadStack state, JsonSeria
 024        {
 025            if (state.ParentProperty?.TryGetPrePopulatedValue(ref state) == true)
 026            {
 027                return;
 28            }
 29
 030            JsonTypeInfo typeInfo = state.Current.JsonTypeInfo;
 031            Func<object>? constructorDelegate = typeInfo.CreateObject;
 32
 033            if (constructorDelegate == null)
 034            {
 035                ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(Type, ref reader, ref state);
 36            }
 37
 038            state.Current.ReturnValue = constructorDelegate();
 39
 040            Debug.Assert(typeInfo.AddMethodDelegate != null);
 041        }
 42
 43        protected sealed override bool OnWriteResume(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions opt
 044        {
 45            IEnumerator enumerator;
 046            if (state.Current.CollectionEnumerator == null)
 047            {
 048                enumerator = value.GetEnumerator();
 049                state.Current.CollectionEnumerator = enumerator;
 050                if (!enumerator.MoveNext())
 051                {
 052                    return true;
 53                }
 054            }
 55            else
 056            {
 057                enumerator = state.Current.CollectionEnumerator;
 058            }
 59
 060            JsonConverter<object?> converter = GetElementConverter(ref state);
 61            do
 062            {
 063                if (ShouldFlush(ref state, writer))
 064                {
 065                    return false;
 66                }
 67
 068                object? element = enumerator.Current;
 069                if (!converter.TryWrite(writer, element, options, ref state))
 070                {
 071                    return false;
 72                }
 73
 074                state.Current.EndCollectionElement();
 075            } while (enumerator.MoveNext());
 76
 077            return true;
 078        }
 79    }
 80}