| | | 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.Collections.Generic; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Runtime.CompilerServices; |
| | | 8 | | using System.Runtime.InteropServices; |
| | | 9 | | using System.Text.Json.Serialization; |
| | | 10 | | using System.Text.Json.Serialization.Metadata; |
| | | 11 | | |
| | | 12 | | namespace System.Text.Json |
| | | 13 | | { |
| | | 14 | | [StructLayout(LayoutKind.Auto)] |
| | | 15 | | [DebuggerDisplay("{DebuggerDisplay,nq}")] |
| | | 16 | | internal struct ReadStackFrame |
| | | 17 | | { |
| | | 18 | | // Current property values. |
| | | 19 | | public JsonPropertyInfo? JsonPropertyInfo; |
| | | 20 | | public StackFramePropertyState PropertyState; |
| | | 21 | | public bool UseExtensionProperty; |
| | | 22 | | |
| | | 23 | | // Support JSON Path on exceptions and non-string Dictionary keys. |
| | | 24 | | // This is Utf8 since we don't want to convert to string until an exception is thrown. |
| | | 25 | | // For dictionary keys we don't want to convert to TKey until we have both key and value when parsing the dictio |
| | | 26 | | public byte[]? JsonPropertyName; |
| | | 27 | | public string? JsonPropertyNameAsString; // This is used for string dictionary keys and re-entry cases that spec |
| | | 28 | | |
| | | 29 | | // Stores the non-string dictionary keys for continuation. |
| | | 30 | | public object? DictionaryKey; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Records the Utf8JsonReader Depth at the start of the current value. |
| | | 34 | | /// </summary> |
| | | 35 | | public int OriginalDepth; |
| | | 36 | | #if DEBUG |
| | | 37 | | /// <summary> |
| | | 38 | | /// Records the Utf8JsonReader TokenType at the start of the current value. |
| | | 39 | | /// Only used to validate debug builds. |
| | | 40 | | /// </summary> |
| | | 41 | | public JsonTokenType OriginalTokenType; |
| | | 42 | | #endif |
| | | 43 | | |
| | | 44 | | // Current object (POCO or IEnumerable). |
| | | 45 | | public object? ReturnValue; // The current return value used for re-entry. |
| | | 46 | | public JsonTypeInfo JsonTypeInfo; |
| | | 47 | | public StackFrameObjectState ObjectState; // State tracking the current object. |
| | | 48 | | |
| | | 49 | | // Current object can contain metadata |
| | | 50 | | public bool CanContainMetadata; |
| | | 51 | | public MetadataPropertyName LatestMetadataPropertyName; |
| | | 52 | | public MetadataPropertyName MetadataPropertyNames; |
| | | 53 | | |
| | | 54 | | // Serialization state for value serialized by the current frame. |
| | | 55 | | public PolymorphicSerializationState PolymorphicSerializationState; |
| | | 56 | | |
| | | 57 | | // Holds any entered polymorphic JsonTypeInfo metadata. |
| | | 58 | | public JsonTypeInfo? PolymorphicJsonTypeInfo; |
| | | 59 | | |
| | | 60 | | // Gets the initial JsonTypeInfo metadata used when deserializing the current value. |
| | | 61 | | public JsonTypeInfo BaseJsonTypeInfo |
| | 0 | 62 | | => PolymorphicSerializationState == PolymorphicSerializationState.PolymorphicReEntryStarted |
| | 0 | 63 | | ? PolymorphicJsonTypeInfo! |
| | 0 | 64 | | : JsonTypeInfo; |
| | | 65 | | |
| | | 66 | | // For performance, we order the properties by the first deserialize and PropertyIndex helps find the right slot |
| | | 67 | | public int PropertyIndex; |
| | | 68 | | |
| | | 69 | | // Tracks newly encounentered UTF-8 encoded properties during the current deserialization, to be appended to the |
| | | 70 | | public PropertyRefCacheBuilder? PropertyRefCacheBuilder; |
| | | 71 | | |
| | | 72 | | // Holds relevant state when deserializing objects with parameterized constructors. |
| | | 73 | | public ArgumentState? CtorArgumentState; |
| | | 74 | | |
| | | 75 | | // Whether to use custom number handling. |
| | | 76 | | public JsonNumberHandling? NumberHandling; |
| | | 77 | | |
| | | 78 | | // Represents known (non-extension) properties which have value assigned. |
| | | 79 | | // Each bit corresponds to a property. |
| | | 80 | | // False means that property is not set (not yet occurred in the payload). |
| | | 81 | | // Length of the BitArray is equal to number of non-extension properties. |
| | | 82 | | // Every JsonPropertyInfo has PropertyIndex property which maps to an index in this BitArray. |
| | | 83 | | public BitArray? AssignedProperties; |
| | | 84 | | |
| | | 85 | | // Tracks state related to property population. |
| | | 86 | | public bool HasParentObject; |
| | | 87 | | public bool IsPopulating; |
| | | 88 | | |
| | | 89 | | public void EndConstructorParameter() |
| | 0 | 90 | | { |
| | 0 | 91 | | CtorArgumentState!.JsonParameterInfo = null; |
| | 0 | 92 | | JsonPropertyName = null; |
| | 0 | 93 | | PropertyState = StackFramePropertyState.None; |
| | 0 | 94 | | } |
| | | 95 | | |
| | | 96 | | public void EndProperty() |
| | 0 | 97 | | { |
| | 0 | 98 | | JsonPropertyInfo = null!; |
| | 0 | 99 | | JsonPropertyName = null; |
| | 0 | 100 | | JsonPropertyNameAsString = null; |
| | 0 | 101 | | PropertyState = StackFramePropertyState.None; |
| | | 102 | | |
| | | 103 | | // No need to clear these since they are overwritten each time: |
| | | 104 | | // NumberHandling |
| | | 105 | | // UseExtensionProperty |
| | 0 | 106 | | } |
| | | 107 | | |
| | | 108 | | public void EndElement() |
| | 10 | 109 | | { |
| | 10 | 110 | | JsonPropertyNameAsString = null; |
| | 10 | 111 | | PropertyState = StackFramePropertyState.None; |
| | 10 | 112 | | } |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Is the current object a Dictionary. |
| | | 116 | | /// </summary> |
| | | 117 | | public bool IsProcessingDictionary() |
| | 0 | 118 | | { |
| | 0 | 119 | | return JsonTypeInfo.Kind is JsonTypeInfoKind.Dictionary; |
| | 0 | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Is the current object an Enumerable. |
| | | 124 | | /// </summary> |
| | | 125 | | public bool IsProcessingEnumerable() |
| | 57506 | 126 | | { |
| | 57506 | 127 | | return JsonTypeInfo.Kind is JsonTypeInfoKind.Enumerable; |
| | 57506 | 128 | | } |
| | | 129 | | |
| | | 130 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 131 | | public void MarkPropertyAsRead(JsonPropertyInfo propertyInfo) |
| | 0 | 132 | | { |
| | 0 | 133 | | if (AssignedProperties is { }) |
| | 0 | 134 | | { |
| | 0 | 135 | | if (!propertyInfo.Options.AllowDuplicateProperties) |
| | 0 | 136 | | { |
| | 0 | 137 | | if (AssignedProperties[propertyInfo.PropertyIndex]) |
| | 0 | 138 | | { |
| | 0 | 139 | | ThrowHelper.ThrowJsonException_DuplicatePropertyNotAllowed(propertyInfo); |
| | | 140 | | } |
| | 0 | 141 | | } |
| | | 142 | | |
| | 0 | 143 | | AssignedProperties[propertyInfo.PropertyIndex] = true; |
| | 0 | 144 | | } |
| | 0 | 145 | | } |
| | | 146 | | |
| | | 147 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 148 | | internal void InitializePropertiesValidationState(JsonTypeInfo typeInfo) |
| | 30 | 149 | | { |
| | 30 | 150 | | Debug.Assert(AssignedProperties is null); |
| | | 151 | | |
| | 30 | 152 | | if (typeInfo.ShouldTrackRequiredProperties || !typeInfo.Options.AllowDuplicateProperties) |
| | 0 | 153 | | { |
| | | 154 | | // This may be slightly larger than required (e.g. if there's an extension property) |
| | 0 | 155 | | AssignedProperties = new BitArray(typeInfo.Properties.Count); |
| | 0 | 156 | | } |
| | 30 | 157 | | } |
| | | 158 | | |
| | | 159 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 160 | | internal void ValidateAllRequiredPropertiesAreRead(JsonTypeInfo typeInfo) |
| | 0 | 161 | | { |
| | 0 | 162 | | if (typeInfo.ShouldTrackRequiredProperties) |
| | 0 | 163 | | { |
| | 0 | 164 | | Debug.Assert(AssignedProperties is not null); |
| | 0 | 165 | | Debug.Assert(typeInfo.OptionalPropertiesMask is not null); |
| | | 166 | | |
| | | 167 | | // All properties must be either assigned or optional |
| | 0 | 168 | | BitArray assignedOrNotRequiredPropertiesSet = AssignedProperties.Or(typeInfo.OptionalPropertiesMask); |
| | | 169 | | |
| | 0 | 170 | | if (!assignedOrNotRequiredPropertiesSet.HasAllSet()) |
| | 0 | 171 | | { |
| | 0 | 172 | | ThrowHelper.ThrowJsonException_JsonRequiredPropertyMissing(typeInfo, assignedOrNotRequiredProperties |
| | | 173 | | } |
| | 0 | 174 | | } |
| | | 175 | | |
| | 0 | 176 | | AssignedProperties = null; |
| | 0 | 177 | | } |
| | | 178 | | |
| | | 179 | | [DebuggerBrowsable(DebuggerBrowsableState.Never)] |
| | 0 | 180 | | private string DebuggerDisplay => $"ConverterStrategy.{JsonTypeInfo?.Converter.ConverterStrategy}, {JsonTypeInfo |
| | | 181 | | } |
| | | 182 | | } |