| | | 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.CompilerServices; |
| | | 7 | | using System.Runtime.InteropServices; |
| | | 8 | | using System.Text.Json.Serialization; |
| | | 9 | | using System.Text.Json.Serialization.Metadata; |
| | | 10 | | |
| | | 11 | | namespace System.Text.Json |
| | | 12 | | { |
| | | 13 | | [StructLayout(LayoutKind.Auto)] |
| | | 14 | | [DebuggerDisplay("{DebuggerDisplay,nq}")] |
| | | 15 | | internal struct ReadStack |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Exposes the stack frame that is currently active. |
| | | 19 | | /// </summary> |
| | | 20 | | public ReadStackFrame Current; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Gets the parent stack frame, if it exists. |
| | | 24 | | /// </summary> |
| | | 25 | | public readonly ref ReadStackFrame Parent |
| | | 26 | | { |
| | | 27 | | get |
| | 0 | 28 | | { |
| | 0 | 29 | | Debug.Assert(_count > 1); |
| | 0 | 30 | | Debug.Assert(_stack is not null); |
| | 0 | 31 | | return ref _stack[_count - 2]; |
| | 0 | 32 | | } |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | public readonly JsonPropertyInfo? ParentProperty |
| | 302 | 36 | | => Current.HasParentObject ? Parent.JsonPropertyInfo : null; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Buffer containing all frames in the stack. For performance it is only populated for serialization depths > 1 |
| | | 40 | | /// </summary> |
| | | 41 | | private ReadStackFrame[] _stack; |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Tracks the current depth of the stack. |
| | | 45 | | /// </summary> |
| | | 46 | | private int _count; |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// If not zero, indicates that the stack is part of a re-entrant continuation of given depth. |
| | | 50 | | /// </summary> |
| | | 51 | | private int _continuationCount; |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Indicates that the state still contains suspended frames waiting re-entry. |
| | | 55 | | /// </summary> |
| | 82012 | 56 | | public readonly bool IsContinuation => _continuationCount != 0; |
| | | 57 | | |
| | | 58 | | // The bag of preservable references. |
| | | 59 | | public ReferenceResolver ReferenceResolver; |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Whether we need to read ahead in the inner read loop. |
| | | 63 | | /// </summary> |
| | | 64 | | public bool SupportContinuation; |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Holds the value of $id or $ref of the currently read object |
| | | 68 | | /// </summary> |
| | | 69 | | public string? ReferenceId; |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Holds the value of $type of the currently read object |
| | | 73 | | /// </summary> |
| | | 74 | | public object? PolymorphicTypeDiscriminator; |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Global flag indicating whether we can read preserved references. |
| | | 78 | | /// </summary> |
| | | 79 | | public bool PreserveReferences; |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Ensures that the stack buffer has sufficient capacity to hold an additional frame. |
| | | 83 | | /// </summary> |
| | | 84 | | private void EnsurePushCapacity() |
| | 4 | 85 | | { |
| | 4 | 86 | | if (_stack is null) |
| | 4 | 87 | | { |
| | 4 | 88 | | _stack = new ReadStackFrame[4]; |
| | 4 | 89 | | } |
| | 0 | 90 | | else if (_count - 1 == _stack.Length) |
| | 0 | 91 | | { |
| | 0 | 92 | | Array.Resize(ref _stack, 2 * _stack.Length); |
| | 0 | 93 | | } |
| | 4 | 94 | | } |
| | | 95 | | |
| | | 96 | | internal void Initialize(JsonTypeInfo jsonTypeInfo, bool supportContinuation = false) |
| | 55924 | 97 | | { |
| | 55924 | 98 | | JsonSerializerOptions options = jsonTypeInfo.Options; |
| | 55924 | 99 | | if (options.ReferenceHandlingStrategy == JsonKnownReferenceHandler.Preserve) |
| | 0 | 100 | | { |
| | 0 | 101 | | ReferenceResolver = options.ReferenceHandler!.CreateResolver(writing: false); |
| | 0 | 102 | | PreserveReferences = true; |
| | 0 | 103 | | } |
| | | 104 | | |
| | 55924 | 105 | | Current.JsonTypeInfo = jsonTypeInfo; |
| | 55924 | 106 | | Current.JsonPropertyInfo = jsonTypeInfo.PropertyInfoForTypeInfo; |
| | 55924 | 107 | | Current.NumberHandling = Current.JsonPropertyInfo.EffectiveNumberHandling; |
| | 55924 | 108 | | Current.CanContainMetadata = PreserveReferences || jsonTypeInfo.PolymorphicTypeResolver?.UsesTypeDiscriminat |
| | 55924 | 109 | | SupportContinuation = supportContinuation; |
| | 55924 | 110 | | } |
| | | 111 | | |
| | | 112 | | public void Push() |
| | 1736 | 113 | | { |
| | 1736 | 114 | | if (_continuationCount == 0) |
| | 1736 | 115 | | { |
| | 1736 | 116 | | if (_count == 0) |
| | 1732 | 117 | | { |
| | | 118 | | // Performance optimization: reuse the first stack frame on the first push operation. |
| | | 119 | | // NB need to be careful when making writes to Current _before_ the first `Push` |
| | | 120 | | // operation is performed. |
| | 1732 | 121 | | _count = 1; |
| | 1732 | 122 | | } |
| | | 123 | | else |
| | 4 | 124 | | { |
| | 4 | 125 | | JsonTypeInfo jsonTypeInfo = Current.JsonPropertyInfo?.JsonTypeInfo ?? Current.CtorArgumentState!.Jso |
| | 4 | 126 | | JsonNumberHandling? numberHandling = Current.NumberHandling; |
| | | 127 | | |
| | 4 | 128 | | EnsurePushCapacity(); |
| | 4 | 129 | | _stack[_count - 1] = Current; |
| | 4 | 130 | | Current = default; |
| | 4 | 131 | | _count++; |
| | | 132 | | |
| | 4 | 133 | | Current.JsonTypeInfo = jsonTypeInfo; |
| | 4 | 134 | | Current.JsonPropertyInfo = jsonTypeInfo.PropertyInfoForTypeInfo; |
| | | 135 | | // Allow number handling on property to win over handling on type. |
| | 4 | 136 | | Current.NumberHandling = numberHandling ?? Current.JsonPropertyInfo.EffectiveNumberHandling; |
| | 4 | 137 | | Current.CanContainMetadata = PreserveReferences || jsonTypeInfo.PolymorphicTypeResolver?.UsesTypeDis |
| | 4 | 138 | | } |
| | 1736 | 139 | | } |
| | | 140 | | else |
| | 0 | 141 | | { |
| | | 142 | | // We are re-entering a continuation, adjust indices accordingly. |
| | | 143 | | |
| | 0 | 144 | | if (_count++ > 0) |
| | 0 | 145 | | { |
| | 0 | 146 | | _stack[_count - 2] = Current; |
| | 0 | 147 | | Current = _stack[_count - 1]; |
| | 0 | 148 | | } |
| | | 149 | | |
| | | 150 | | // check if we are done |
| | 0 | 151 | | if (_continuationCount == _count) |
| | 0 | 152 | | { |
| | 0 | 153 | | _continuationCount = 0; |
| | 0 | 154 | | } |
| | 0 | 155 | | } |
| | | 156 | | |
| | 1736 | 157 | | SetConstructorArgumentState(); |
| | | 158 | | #if DEBUG |
| | | 159 | | // Ensure the method is always exercised in debug builds. |
| | 1736 | 160 | | _ = JsonPath(); |
| | | 161 | | #endif |
| | 1736 | 162 | | } |
| | | 163 | | |
| | | 164 | | public void Pop(bool success) |
| | 34 | 165 | | { |
| | 34 | 166 | | Debug.Assert(_count > 0); |
| | 34 | 167 | | Debug.Assert(JsonPath() is not null); |
| | | 168 | | |
| | 34 | 169 | | if (!success) |
| | 0 | 170 | | { |
| | | 171 | | // Check if we need to initialize the continuation. |
| | 0 | 172 | | if (_continuationCount == 0) |
| | 0 | 173 | | { |
| | 0 | 174 | | if (_count == 1) |
| | 0 | 175 | | { |
| | | 176 | | // No need to copy any frames here. |
| | 0 | 177 | | _continuationCount = 1; |
| | 0 | 178 | | _count = 0; |
| | 0 | 179 | | return; |
| | | 180 | | } |
| | | 181 | | |
| | | 182 | | // Need to push the Current frame to the stack, |
| | | 183 | | // ensure that we have sufficient capacity. |
| | 0 | 184 | | EnsurePushCapacity(); |
| | 0 | 185 | | _continuationCount = _count--; |
| | 0 | 186 | | } |
| | 0 | 187 | | else if (--_count == 0) |
| | 0 | 188 | | { |
| | | 189 | | // reached the root, no need to copy frames. |
| | 0 | 190 | | return; |
| | | 191 | | } |
| | | 192 | | |
| | 0 | 193 | | _stack[_count] = Current; |
| | 0 | 194 | | Current = _stack[_count - 1]; |
| | 0 | 195 | | } |
| | | 196 | | else |
| | 34 | 197 | | { |
| | 34 | 198 | | Debug.Assert(_continuationCount == 0); |
| | | 199 | | |
| | 34 | 200 | | if (--_count > 0) |
| | 0 | 201 | | { |
| | 0 | 202 | | Current = _stack[_count - 1]; |
| | 0 | 203 | | } |
| | 34 | 204 | | } |
| | 34 | 205 | | } |
| | | 206 | | |
| | | 207 | | /// <summary> |
| | | 208 | | /// Configures the current stack frame for a polymorphic converter. |
| | | 209 | | /// </summary> |
| | | 210 | | public JsonConverter InitializePolymorphicReEntry(JsonTypeInfo derivedJsonTypeInfo) |
| | 0 | 211 | | { |
| | 0 | 212 | | Debug.Assert(!IsContinuation); |
| | 0 | 213 | | Debug.Assert(Current.PolymorphicJsonTypeInfo == null); |
| | 0 | 214 | | Debug.Assert(Current.PolymorphicSerializationState == PolymorphicSerializationState.None); |
| | | 215 | | |
| | 0 | 216 | | Current.PolymorphicJsonTypeInfo = Current.JsonTypeInfo; |
| | 0 | 217 | | Current.JsonTypeInfo = derivedJsonTypeInfo; |
| | 0 | 218 | | Current.JsonPropertyInfo = derivedJsonTypeInfo.PropertyInfoForTypeInfo; |
| | 0 | 219 | | Current.NumberHandling ??= Current.JsonPropertyInfo.NumberHandling; |
| | 0 | 220 | | Current.PolymorphicSerializationState = PolymorphicSerializationState.PolymorphicReEntryStarted; |
| | 0 | 221 | | SetConstructorArgumentState(); |
| | | 222 | | |
| | 0 | 223 | | return derivedJsonTypeInfo.Converter; |
| | 0 | 224 | | } |
| | | 225 | | |
| | | 226 | | |
| | | 227 | | /// <summary> |
| | | 228 | | /// Configures the current frame for a continuation of a polymorphic converter. |
| | | 229 | | /// </summary> |
| | | 230 | | public JsonConverter ResumePolymorphicReEntry() |
| | 0 | 231 | | { |
| | 0 | 232 | | Debug.Assert(Current.PolymorphicJsonTypeInfo != null); |
| | 0 | 233 | | Debug.Assert(Current.PolymorphicSerializationState == PolymorphicSerializationState.PolymorphicReEntrySuspen |
| | | 234 | | |
| | | 235 | | // Swap out the two values as we resume the polymorphic converter |
| | 0 | 236 | | (Current.JsonTypeInfo, Current.PolymorphicJsonTypeInfo) = (Current.PolymorphicJsonTypeInfo, Current.JsonType |
| | 0 | 237 | | Current.PolymorphicSerializationState = PolymorphicSerializationState.PolymorphicReEntryStarted; |
| | 0 | 238 | | return Current.JsonTypeInfo.Converter; |
| | 0 | 239 | | } |
| | | 240 | | |
| | | 241 | | /// <summary> |
| | | 242 | | /// Updates frame state after a polymorphic converter has returned. |
| | | 243 | | /// </summary> |
| | | 244 | | public void ExitPolymorphicConverter(bool success) |
| | 0 | 245 | | { |
| | 0 | 246 | | Debug.Assert(Current.PolymorphicJsonTypeInfo != null); |
| | 0 | 247 | | Debug.Assert(Current.PolymorphicSerializationState == PolymorphicSerializationState.PolymorphicReEntryStarte |
| | | 248 | | |
| | | 249 | | // Swap out the two values as we exit the polymorphic converter |
| | 0 | 250 | | (Current.JsonTypeInfo, Current.PolymorphicJsonTypeInfo) = (Current.PolymorphicJsonTypeInfo, Current.JsonType |
| | 0 | 251 | | Current.PolymorphicSerializationState = success ? PolymorphicSerializationState.None : PolymorphicSerializat |
| | 0 | 252 | | } |
| | | 253 | | |
| | | 254 | | // Return a JSONPath using simple dot-notation when possible. When special characters are present, bracket-notat |
| | | 255 | | // $.x.y[0].z |
| | | 256 | | // $['PropertyName.With.Special.Chars'] |
| | | 257 | | public string JsonPath() |
| | 57498 | 258 | | { |
| | 57498 | 259 | | StringBuilder sb = new StringBuilder("$"); |
| | | 260 | | |
| | 57498 | 261 | | (int frameCount, bool includeCurrentFrame) = _continuationCount switch |
| | 57498 | 262 | | { |
| | 57498 | 263 | | 0 => (_count - 1, true), // Not a continuation, report previous frames and Current. |
| | 0 | 264 | | 1 => (0, true), // Continuation of depth 1, just report Current frame. |
| | 0 | 265 | | int c => (c, false) // Continuation of depth > 1, report the entire stack. |
| | 57498 | 266 | | }; |
| | | 267 | | |
| | 115012 | 268 | | for (int i = 0; i < frameCount; i++) |
| | 8 | 269 | | { |
| | 8 | 270 | | AppendStackFrame(sb, ref _stack[i]); |
| | 8 | 271 | | } |
| | | 272 | | |
| | 57498 | 273 | | if (includeCurrentFrame) |
| | 57498 | 274 | | { |
| | 57498 | 275 | | AppendStackFrame(sb, ref Current); |
| | 57498 | 276 | | } |
| | | 277 | | |
| | 57498 | 278 | | return sb.ToString(); |
| | | 279 | | |
| | | 280 | | static void AppendStackFrame(StringBuilder sb, ref ReadStackFrame frame) |
| | 57506 | 281 | | { |
| | | 282 | | // Append the property name. |
| | 57506 | 283 | | string? propertyName = GetPropertyName(ref frame); |
| | 57506 | 284 | | AppendPropertyName(sb, propertyName); |
| | | 285 | | |
| | 57506 | 286 | | if (frame.JsonTypeInfo != null && frame.IsProcessingEnumerable()) |
| | 2276 | 287 | | { |
| | 2276 | 288 | | if (frame.ReturnValue is not IEnumerable enumerable) |
| | 2026 | 289 | | { |
| | 2026 | 290 | | return; |
| | | 291 | | } |
| | | 292 | | |
| | | 293 | | // For continuation scenarios only, before or after all elements are read, the exception is not with |
| | 250 | 294 | | if (frame.ObjectState == StackFrameObjectState.None || |
| | 250 | 295 | | frame.ObjectState == StackFrameObjectState.CreatedObject || |
| | 250 | 296 | | frame.ObjectState == StackFrameObjectState.ReadElements) |
| | 182 | 297 | | { |
| | 182 | 298 | | sb.Append('['); |
| | 182 | 299 | | sb.Append(GetCount(enumerable)); |
| | 182 | 300 | | sb.Append(']'); |
| | 182 | 301 | | } |
| | 250 | 302 | | } |
| | 57506 | 303 | | } |
| | | 304 | | |
| | | 305 | | static int GetCount(IEnumerable enumerable) |
| | 182 | 306 | | { |
| | 182 | 307 | | if (enumerable is ICollection collection) |
| | 182 | 308 | | { |
| | 182 | 309 | | return collection.Count; |
| | | 310 | | } |
| | | 311 | | |
| | 0 | 312 | | int count = 0; |
| | 0 | 313 | | IEnumerator enumerator = enumerable.GetEnumerator(); |
| | 0 | 314 | | while (enumerator.MoveNext()) |
| | 0 | 315 | | { |
| | 0 | 316 | | count++; |
| | 0 | 317 | | } |
| | | 318 | | |
| | 0 | 319 | | return count; |
| | 182 | 320 | | } |
| | | 321 | | |
| | | 322 | | static void AppendPropertyName(StringBuilder sb, string? propertyName) |
| | 57506 | 323 | | { |
| | 57506 | 324 | | if (propertyName != null) |
| | 0 | 325 | | { |
| | 0 | 326 | | if (propertyName.AsSpan().ContainsSpecialCharacters()) |
| | 0 | 327 | | { |
| | 0 | 328 | | sb.Append(@"['"); |
| | 0 | 329 | | sb.AppendEscapedPropertyName(propertyName); |
| | 0 | 330 | | sb.Append(@"']"); |
| | 0 | 331 | | } |
| | | 332 | | else |
| | 0 | 333 | | { |
| | 0 | 334 | | sb.Append('.'); |
| | 0 | 335 | | sb.Append(propertyName); |
| | 0 | 336 | | } |
| | 0 | 337 | | } |
| | 57506 | 338 | | } |
| | | 339 | | |
| | | 340 | | static string? GetPropertyName(ref ReadStackFrame frame) |
| | 57506 | 341 | | { |
| | 57506 | 342 | | string? propertyName = null; |
| | | 343 | | |
| | | 344 | | // Attempt to get the JSON property name from the frame. |
| | 57506 | 345 | | byte[]? utf8PropertyName = frame.JsonPropertyName; |
| | 57506 | 346 | | if (utf8PropertyName == null) |
| | 57506 | 347 | | { |
| | 57506 | 348 | | if (frame.JsonPropertyNameAsString != null) |
| | 0 | 349 | | { |
| | | 350 | | // Attempt to get the JSON property name set manually for dictionary |
| | | 351 | | // keys and KeyValuePair property names. |
| | 0 | 352 | | propertyName = frame.JsonPropertyNameAsString; |
| | 0 | 353 | | } |
| | | 354 | | else |
| | 57506 | 355 | | { |
| | | 356 | | // Attempt to get the JSON property name from the JsonPropertyInfo or JsonParameterInfo. |
| | 57506 | 357 | | utf8PropertyName = frame.JsonPropertyInfo?.NameAsUtf8Bytes ?? |
| | 57506 | 358 | | frame.CtorArgumentState?.JsonParameterInfo?.JsonNameAsUtf8Bytes; |
| | 57506 | 359 | | } |
| | 57506 | 360 | | } |
| | | 361 | | |
| | 57506 | 362 | | if (utf8PropertyName != null) |
| | 0 | 363 | | { |
| | 0 | 364 | | propertyName = Encoding.UTF8.GetString(utf8PropertyName); |
| | 0 | 365 | | } |
| | | 366 | | |
| | 57506 | 367 | | return propertyName; |
| | 57506 | 368 | | } |
| | 57498 | 369 | | } |
| | | 370 | | |
| | | 371 | | // Traverses the stack for the outermost object being deserialized using constructor parameters |
| | | 372 | | // Only called when calculating exception information. |
| | | 373 | | public JsonTypeInfo GetTopJsonTypeInfoWithParameterizedConstructor() |
| | 0 | 374 | | { |
| | 0 | 375 | | Debug.Assert(!IsContinuation); |
| | | 376 | | |
| | 0 | 377 | | for (int i = 0; i < _count - 1; i++) |
| | 0 | 378 | | { |
| | 0 | 379 | | if (_stack[i].JsonTypeInfo.UsesParameterizedConstructor) |
| | 0 | 380 | | { |
| | 0 | 381 | | return _stack[i].JsonTypeInfo; |
| | | 382 | | } |
| | 0 | 383 | | } |
| | | 384 | | |
| | 0 | 385 | | Debug.Assert(Current.JsonTypeInfo.UsesParameterizedConstructor); |
| | 0 | 386 | | return Current.JsonTypeInfo; |
| | 0 | 387 | | } |
| | | 388 | | |
| | | 389 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 390 | | private void SetConstructorArgumentState() |
| | 1736 | 391 | | { |
| | 1736 | 392 | | if (Current.JsonTypeInfo.UsesParameterizedConstructor) |
| | 234 | 393 | | { |
| | 234 | 394 | | Current.CtorArgumentState ??= new(); |
| | 234 | 395 | | } |
| | 1736 | 396 | | } |
| | | 397 | | |
| | | 398 | | [DebuggerBrowsable(DebuggerBrowsableState.Never)] |
| | 0 | 399 | | private string DebuggerDisplay => $"Path = {JsonPath()}, Current = ConverterStrategy.{Current.JsonTypeInfo?.Conv |
| | | 400 | | } |
| | | 401 | | } |