< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 44
Coverable lines: 44
Total lines: 171
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
EndCollectionElement()100%110%
EndDictionaryEntry()100%110%
EndProperty()100%110%
GetNestedJsonTypeInfo()0%220%
InitializePolymorphicReEntry(...)0%660%
InitializePolymorphicReEntry(...)0%220%
ResumePolymorphicReEntry()100%110%
ExitPolymorphicConverter(...)0%220%

File(s)

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\WriteStackFrame.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.Runtime.InteropServices;
 7using System.Text.Json.Serialization;
 8using System.Text.Json.Serialization.Metadata;
 9
 10namespace System.Text.Json
 11{
 12    [StructLayout(LayoutKind.Auto)]
 13    [DebuggerDisplay("{DebuggerDisplay,nq}")]
 14    internal struct WriteStackFrame
 15    {
 16        /// <summary>
 17        /// The enumerator for resumable collections.
 18        /// </summary>
 19        public IEnumerator? CollectionEnumerator;
 20
 21        /// <summary>
 22        /// The enumerator for resumable async disposables.
 23        /// </summary>
 24        public IAsyncDisposable? AsyncDisposable;
 25
 26        /// <summary>
 27        /// The current stackframe has suspended serialization due to a pending task,
 28        /// stored in the <see cref="WriteStack.PendingTask"/> property.
 29        /// </summary>
 30        public bool AsyncEnumeratorIsPendingCompletion;
 31
 32        /// <summary>
 33        /// The original JsonPropertyInfo that is not changed. It contains all properties.
 34        /// </summary>
 35        /// <remarks>
 36        /// For objects, it is either the actual (real) JsonPropertyInfo or the <see cref="JsonTypeInfo.PropertyInfoForT
 37        /// For collections, it is the <see cref="JsonTypeInfo.PropertyInfoForTypeInfo"/> for the class and current elem
 38        /// </remarks>
 39        public JsonPropertyInfo? JsonPropertyInfo;
 40
 41        /// <summary>
 42        /// Used when processing extension data dictionaries.
 43        /// </summary>
 44        public bool IsWritingExtensionDataProperty;
 45
 46        /// <summary>
 47        /// The class (POCO or IEnumerable) that is being populated.
 48        /// </summary>
 49        public JsonTypeInfo JsonTypeInfo;
 50
 51        /// <summary>
 52        /// Validation state for a class.
 53        /// </summary>
 54        public int OriginalDepth;
 55
 56        // Class-level state for collections.
 57        public bool ProcessedStartToken;
 58        public bool ProcessedEndToken;
 59
 60        /// <summary>
 61        /// Property or Element state.
 62        /// </summary>
 63        public StackFramePropertyState PropertyState;
 64
 65        /// <summary>
 66        /// The enumerator index for resumable collections.
 67        /// </summary>
 68        public int EnumeratorIndex;
 69
 70        // This is used for re-entry cases for exception handling.
 71        public string? JsonPropertyNameAsString;
 72
 73        // Preserve Reference
 74        public MetadataPropertyName MetadataPropertyName;
 75
 76        // Serialization state for the child value serialized by the current frame.
 77        public PolymorphicSerializationState PolymorphicSerializationState;
 78        // Holds the entered polymorphic type info and acts as an LRU cache for element/field serializations.
 79        public JsonTypeInfo? PolymorphicTypeInfo;
 80
 81        // Whether to use custom number handling.
 82        public JsonNumberHandling? NumberHandling;
 83
 84        public bool IsPushedReferenceForCycleDetection;
 85
 86        public void EndCollectionElement()
 087        {
 088            PolymorphicSerializationState = PolymorphicSerializationState.None;
 089        }
 90
 91        public void EndDictionaryEntry()
 092        {
 093            PropertyState = StackFramePropertyState.None;
 094            PolymorphicSerializationState = PolymorphicSerializationState.None;
 095        }
 96
 97        public void EndProperty()
 098        {
 099            JsonPropertyInfo = null!;
 0100            JsonPropertyNameAsString = null;
 0101            PropertyState = StackFramePropertyState.None;
 0102            PolymorphicSerializationState = PolymorphicSerializationState.None;
 0103        }
 104
 105        /// <summary>
 106        /// Returns the JsonTypeInfo instance for the nested value we are trying to access.
 107        /// </summary>
 108        public readonly JsonTypeInfo GetNestedJsonTypeInfo()
 0109        {
 0110            return PolymorphicSerializationState is PolymorphicSerializationState.PolymorphicReEntryStarted
 0111                ? PolymorphicTypeInfo!
 0112                : JsonPropertyInfo!.JsonTypeInfo;
 0113        }
 114
 115        /// <summary>
 116        /// Configures the next stack frame for a polymorphic converter.
 117        /// </summary>
 118        public JsonTypeInfo InitializePolymorphicReEntry(Type runtimeType, JsonSerializerOptions options)
 0119        {
 0120            Debug.Assert(PolymorphicSerializationState == PolymorphicSerializationState.None);
 121
 122            // For perf, avoid the dictionary lookup in GetTypeInfoInternal() for every element of a collection
 123            // if the current element is the same type as the previous element.
 0124            if (PolymorphicTypeInfo?.Type != runtimeType)
 0125            {
 126                // To determine the contract for an object value:
 127                // 1. Find the JsonTypeInfo for the runtime type with fallback to the nearest ancestor, if not available
 128                // 2. If the resolved type is deriving from a polymorphic type, use the contract of the polymorphic type
 0129                JsonTypeInfo typeInfo = options.GetTypeInfoInternal(runtimeType, fallBackToNearestAncestorType: true);
 0130                PolymorphicTypeInfo = typeInfo.AncestorPolymorphicType ?? typeInfo;
 0131            }
 132
 0133            PolymorphicSerializationState = PolymorphicSerializationState.PolymorphicReEntryStarted;
 0134            return PolymorphicTypeInfo;
 0135        }
 136
 137        /// <summary>
 138        /// Configures the next stack frame for a polymorphic converter.
 139        /// </summary>
 140        public JsonConverter InitializePolymorphicReEntry(JsonTypeInfo derivedJsonTypeInfo)
 0141        {
 0142            Debug.Assert(PolymorphicSerializationState is PolymorphicSerializationState.None or PolymorphicSerialization
 143
 0144            PolymorphicTypeInfo = derivedJsonTypeInfo;
 0145            PolymorphicSerializationState = PolymorphicSerializationState.PolymorphicReEntryStarted;
 0146            return derivedJsonTypeInfo.Converter;
 0147        }
 148
 149        /// <summary>
 150        /// Configures the next frame for a continuation of a polymorphic converter.
 151        /// </summary>
 152        public JsonConverter ResumePolymorphicReEntry()
 0153        {
 0154            Debug.Assert(PolymorphicSerializationState == PolymorphicSerializationState.PolymorphicReEntrySuspended);
 0155            Debug.Assert(PolymorphicTypeInfo is not null);
 0156            PolymorphicSerializationState = PolymorphicSerializationState.PolymorphicReEntryStarted;
 0157            return PolymorphicTypeInfo.Converter;
 0158        }
 159
 160        /// <summary>
 161        /// Updates frame state after a polymorphic converter has returned.
 162        /// </summary>
 163        public void ExitPolymorphicConverter(bool success)
 0164        {
 0165            PolymorphicSerializationState = success ? PolymorphicSerializationState.None : PolymorphicSerializationState
 0166        }
 167
 168        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
 0169        private readonly string DebuggerDisplay => $"ConverterStrategy.{JsonTypeInfo?.Converter.ConverterStrategy}, {Jso
 170    }
 171}