| | | 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 | | |
| | | 4 | | using System.Collections; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Runtime.InteropServices; |
| | | 7 | | using System.Text.Json.Serialization; |
| | | 8 | | using System.Text.Json.Serialization.Metadata; |
| | | 9 | | |
| | | 10 | | namespace 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() |
| | 0 | 87 | | { |
| | 0 | 88 | | PolymorphicSerializationState = PolymorphicSerializationState.None; |
| | 0 | 89 | | } |
| | | 90 | | |
| | | 91 | | public void EndDictionaryEntry() |
| | 0 | 92 | | { |
| | 0 | 93 | | PropertyState = StackFramePropertyState.None; |
| | 0 | 94 | | PolymorphicSerializationState = PolymorphicSerializationState.None; |
| | 0 | 95 | | } |
| | | 96 | | |
| | | 97 | | public void EndProperty() |
| | 0 | 98 | | { |
| | 0 | 99 | | JsonPropertyInfo = null!; |
| | 0 | 100 | | JsonPropertyNameAsString = null; |
| | 0 | 101 | | PropertyState = StackFramePropertyState.None; |
| | 0 | 102 | | PolymorphicSerializationState = PolymorphicSerializationState.None; |
| | 0 | 103 | | } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Returns the JsonTypeInfo instance for the nested value we are trying to access. |
| | | 107 | | /// </summary> |
| | | 108 | | public readonly JsonTypeInfo GetNestedJsonTypeInfo() |
| | 0 | 109 | | { |
| | 0 | 110 | | return PolymorphicSerializationState is PolymorphicSerializationState.PolymorphicReEntryStarted |
| | 0 | 111 | | ? PolymorphicTypeInfo! |
| | 0 | 112 | | : JsonPropertyInfo!.JsonTypeInfo; |
| | 0 | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Configures the next stack frame for a polymorphic converter. |
| | | 117 | | /// </summary> |
| | | 118 | | public JsonTypeInfo InitializePolymorphicReEntry(Type runtimeType, JsonSerializerOptions options) |
| | 0 | 119 | | { |
| | 0 | 120 | | 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. |
| | 0 | 124 | | if (PolymorphicTypeInfo?.Type != runtimeType) |
| | 0 | 125 | | { |
| | | 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 |
| | 0 | 129 | | JsonTypeInfo typeInfo = options.GetTypeInfoInternal(runtimeType, fallBackToNearestAncestorType: true); |
| | 0 | 130 | | PolymorphicTypeInfo = typeInfo.AncestorPolymorphicType ?? typeInfo; |
| | 0 | 131 | | } |
| | | 132 | | |
| | 0 | 133 | | PolymorphicSerializationState = PolymorphicSerializationState.PolymorphicReEntryStarted; |
| | 0 | 134 | | return PolymorphicTypeInfo; |
| | 0 | 135 | | } |
| | | 136 | | |
| | | 137 | | /// <summary> |
| | | 138 | | /// Configures the next stack frame for a polymorphic converter. |
| | | 139 | | /// </summary> |
| | | 140 | | public JsonConverter InitializePolymorphicReEntry(JsonTypeInfo derivedJsonTypeInfo) |
| | 0 | 141 | | { |
| | 0 | 142 | | Debug.Assert(PolymorphicSerializationState is PolymorphicSerializationState.None or PolymorphicSerialization |
| | | 143 | | |
| | 0 | 144 | | PolymorphicTypeInfo = derivedJsonTypeInfo; |
| | 0 | 145 | | PolymorphicSerializationState = PolymorphicSerializationState.PolymorphicReEntryStarted; |
| | 0 | 146 | | return derivedJsonTypeInfo.Converter; |
| | 0 | 147 | | } |
| | | 148 | | |
| | | 149 | | /// <summary> |
| | | 150 | | /// Configures the next frame for a continuation of a polymorphic converter. |
| | | 151 | | /// </summary> |
| | | 152 | | public JsonConverter ResumePolymorphicReEntry() |
| | 0 | 153 | | { |
| | 0 | 154 | | Debug.Assert(PolymorphicSerializationState == PolymorphicSerializationState.PolymorphicReEntrySuspended); |
| | 0 | 155 | | Debug.Assert(PolymorphicTypeInfo is not null); |
| | 0 | 156 | | PolymorphicSerializationState = PolymorphicSerializationState.PolymorphicReEntryStarted; |
| | 0 | 157 | | return PolymorphicTypeInfo.Converter; |
| | 0 | 158 | | } |
| | | 159 | | |
| | | 160 | | /// <summary> |
| | | 161 | | /// Updates frame state after a polymorphic converter has returned. |
| | | 162 | | /// </summary> |
| | | 163 | | public void ExitPolymorphicConverter(bool success) |
| | 0 | 164 | | { |
| | 0 | 165 | | PolymorphicSerializationState = success ? PolymorphicSerializationState.None : PolymorphicSerializationState |
| | 0 | 166 | | } |
| | | 167 | | |
| | | 168 | | [DebuggerBrowsable(DebuggerBrowsableState.Never)] |
| | 0 | 169 | | private readonly string DebuggerDisplay => $"ConverterStrategy.{JsonTypeInfo?.Converter.ConverterStrategy}, {Jso |
| | | 170 | | } |
| | | 171 | | } |