| | | 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.Buffers; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Diagnostics.CodeAnalysis; |
| | | 8 | | using System.Runtime.CompilerServices; |
| | | 9 | | using System.Text.Json.Serialization.Metadata; |
| | | 10 | | |
| | | 11 | | using FoundProperty = System.ValueTuple<System.Text.Json.Serialization.Metadata.JsonPropertyInfo, System.Text.Json.JsonR |
| | | 12 | | using FoundPropertyAsync = System.ValueTuple<System.Text.Json.Serialization.Metadata.JsonPropertyInfo, object?, string?> |
| | | 13 | | |
| | | 14 | | namespace System.Text.Json.Serialization.Converters |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Implementation of <cref>JsonObjectConverter{T}</cref> that supports the deserialization |
| | | 18 | | /// of JSON objects using parameterized constructors. |
| | | 19 | | /// </summary> |
| | | 20 | | internal abstract partial class ObjectWithParameterizedConstructorConverter<T> : ObjectDefaultConverter<T> where T : |
| | | 21 | | { |
| | 8988 | 22 | | internal sealed override bool ConstructorIsParameterized => true; |
| | | 23 | | |
| | | 24 | | internal sealed override bool OnTryRead(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions opt |
| | 234 | 25 | | { |
| | 234 | 26 | | JsonTypeInfo jsonTypeInfo = state.Current.JsonTypeInfo; |
| | | 27 | | |
| | 234 | 28 | | if (!jsonTypeInfo.UsesParameterizedConstructor || state.Current.IsPopulating) |
| | 0 | 29 | | { |
| | | 30 | | // Fall back to default object converter in following cases: |
| | | 31 | | // - if user configuration has invalidated the parameterized constructor |
| | | 32 | | // - we're continuing populating an object. |
| | 0 | 33 | | return base.OnTryRead(ref reader, typeToConvert, options, ref state, out value); |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | object obj; |
| | 234 | 37 | | ArgumentState argumentState = state.Current.CtorArgumentState!; |
| | | 38 | | |
| | 234 | 39 | | if (!state.SupportContinuation && !state.Current.CanContainMetadata) |
| | 0 | 40 | | { |
| | | 41 | | // Fast path that avoids maintaining state variables. |
| | | 42 | | |
| | 0 | 43 | | if (reader.TokenType != JsonTokenType.StartObject) |
| | 0 | 44 | | { |
| | 0 | 45 | | ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type); |
| | | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | if (state.ParentProperty?.TryGetPrePopulatedValue(ref state) == true) |
| | 0 | 49 | | { |
| | 0 | 50 | | object populatedObject = state.Current.ReturnValue!; |
| | 0 | 51 | | PopulatePropertiesFastPath(populatedObject, jsonTypeInfo, options, ref reader, ref state); |
| | 0 | 52 | | value = (T)populatedObject; |
| | 0 | 53 | | return true; |
| | | 54 | | } |
| | | 55 | | |
| | 0 | 56 | | ReadOnlySpan<byte> originalSpan = reader.OriginalSpan; |
| | 0 | 57 | | ReadOnlySequence<byte> originalSequence = reader.OriginalSequence; |
| | | 58 | | |
| | 0 | 59 | | ReadConstructorArguments(ref state, ref reader, options); |
| | | 60 | | |
| | | 61 | | // We've read all ctor parameters and properties, |
| | | 62 | | // validate that all required parameters were provided |
| | | 63 | | // before calling the constructor which may throw. |
| | 0 | 64 | | state.Current.ValidateAllRequiredPropertiesAreRead(jsonTypeInfo); |
| | | 65 | | |
| | 0 | 66 | | obj = (T)CreateObject(ref state.Current); |
| | | 67 | | |
| | 0 | 68 | | jsonTypeInfo.OnDeserializing?.Invoke(obj); |
| | | 69 | | |
| | 0 | 70 | | if (argumentState.FoundPropertyCount > 0) |
| | 0 | 71 | | { |
| | | 72 | | Utf8JsonReader tempReader; |
| | | 73 | | |
| | 0 | 74 | | FoundProperty[]? properties = argumentState.FoundProperties; |
| | 0 | 75 | | Debug.Assert(properties != null); |
| | | 76 | | |
| | 0 | 77 | | for (int i = 0; i < argumentState.FoundPropertyCount; i++) |
| | 0 | 78 | | { |
| | 0 | 79 | | JsonPropertyInfo jsonPropertyInfo = properties[i].Item1; |
| | 0 | 80 | | long resumptionByteIndex = properties[i].Item3; |
| | 0 | 81 | | byte[]? propertyNameArray = properties[i].Item4; |
| | 0 | 82 | | string? dataExtKey = properties[i].Item5; |
| | | 83 | | |
| | 0 | 84 | | tempReader = originalSequence.IsEmpty |
| | 0 | 85 | | ? new Utf8JsonReader( |
| | 0 | 86 | | originalSpan.Slice(checked((int)resumptionByteIndex)), |
| | 0 | 87 | | isFinalBlock: true, |
| | 0 | 88 | | state: properties[i].Item2) |
| | 0 | 89 | | : new Utf8JsonReader( |
| | 0 | 90 | | originalSequence.Slice(resumptionByteIndex), |
| | 0 | 91 | | isFinalBlock: true, |
| | 0 | 92 | | state: properties[i].Item2); |
| | | 93 | | |
| | 0 | 94 | | Debug.Assert(tempReader.TokenType == JsonTokenType.PropertyName); |
| | | 95 | | |
| | 0 | 96 | | state.Current.JsonPropertyName = propertyNameArray; |
| | 0 | 97 | | state.Current.JsonPropertyInfo = jsonPropertyInfo; |
| | 0 | 98 | | state.Current.NumberHandling = jsonPropertyInfo.EffectiveNumberHandling; |
| | | 99 | | |
| | 0 | 100 | | bool useExtensionProperty = dataExtKey != null; |
| | | 101 | | |
| | 0 | 102 | | if (useExtensionProperty) |
| | 0 | 103 | | { |
| | 0 | 104 | | Debug.Assert(jsonPropertyInfo == state.Current.JsonTypeInfo.ExtensionDataProperty); |
| | 0 | 105 | | state.Current.JsonPropertyNameAsString = dataExtKey; |
| | 0 | 106 | | JsonSerializer.CreateExtensionDataProperty(obj, jsonPropertyInfo, options); |
| | 0 | 107 | | } |
| | | 108 | | |
| | 0 | 109 | | ReadPropertyValue(obj, ref state, ref tempReader, jsonPropertyInfo, useExtensionProperty); |
| | 0 | 110 | | } |
| | | 111 | | |
| | 0 | 112 | | FoundProperty[] toReturn = argumentState.FoundProperties!; |
| | 0 | 113 | | argumentState.FoundProperties = null; |
| | 0 | 114 | | ArrayPool<FoundProperty>.Shared.Return(toReturn, clearArray: true); |
| | 0 | 115 | | } |
| | 0 | 116 | | } |
| | | 117 | | else |
| | 234 | 118 | | { |
| | | 119 | | // Slower path that supports continuation and metadata reads. |
| | | 120 | | |
| | 234 | 121 | | if (state.Current.ObjectState == StackFrameObjectState.None) |
| | 234 | 122 | | { |
| | 234 | 123 | | if (reader.TokenType != JsonTokenType.StartObject) |
| | 226 | 124 | | { |
| | 226 | 125 | | ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type); |
| | | 126 | | } |
| | | 127 | | |
| | 8 | 128 | | state.Current.ObjectState = StackFrameObjectState.StartToken; |
| | 8 | 129 | | } |
| | | 130 | | |
| | | 131 | | // Read any metadata properties. |
| | 8 | 132 | | if (state.Current.CanContainMetadata && state.Current.ObjectState < StackFrameObjectState.ReadMetadata) |
| | 0 | 133 | | { |
| | 0 | 134 | | if (!JsonSerializer.TryReadMetadata(this, jsonTypeInfo, ref reader, ref state)) |
| | 0 | 135 | | { |
| | 0 | 136 | | value = default; |
| | 0 | 137 | | return false; |
| | | 138 | | } |
| | | 139 | | |
| | 0 | 140 | | if (state.Current.MetadataPropertyNames == MetadataPropertyName.Ref) |
| | 0 | 141 | | { |
| | 0 | 142 | | value = JsonSerializer.ResolveReferenceId<T>(ref state); |
| | 0 | 143 | | return true; |
| | | 144 | | } |
| | | 145 | | |
| | 0 | 146 | | state.Current.ObjectState = StackFrameObjectState.ReadMetadata; |
| | 0 | 147 | | } |
| | | 148 | | |
| | | 149 | | // Dispatch to any polymorphic converters: should always be entered regardless of ObjectState progress |
| | 8 | 150 | | if ((state.Current.MetadataPropertyNames & MetadataPropertyName.Type) != 0 && |
| | 8 | 151 | | state.Current.PolymorphicSerializationState != PolymorphicSerializationState.PolymorphicReEntryStart |
| | 8 | 152 | | ResolvePolymorphicConverter(jsonTypeInfo, ref state) is JsonConverter polymorphicConverter) |
| | 0 | 153 | | { |
| | 0 | 154 | | Debug.Assert(!IsValueType); |
| | 0 | 155 | | bool success = polymorphicConverter.OnTryReadAsObject(ref reader, polymorphicConverter.Type!, option |
| | 0 | 156 | | value = (T)objectResult!; |
| | 0 | 157 | | state.ExitPolymorphicConverter(success); |
| | 0 | 158 | | return success; |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | // We need to populate before we started reading constructor arguments. |
| | | 162 | | // Metadata is disallowed with Populate option and therefore ordering here is irrelevant. |
| | | 163 | | // Since state.Current.IsPopulating is being checked early on in this method the continuation |
| | | 164 | | // will be handled there. |
| | 8 | 165 | | if (state.ParentProperty?.TryGetPrePopulatedValue(ref state) == true) |
| | 0 | 166 | | { |
| | 0 | 167 | | object populatedObject = state.Current.ReturnValue!; |
| | | 168 | | |
| | 0 | 169 | | jsonTypeInfo.OnDeserializing?.Invoke(populatedObject); |
| | 0 | 170 | | state.Current.ObjectState = StackFrameObjectState.CreatedObject; |
| | 0 | 171 | | state.Current.InitializePropertiesValidationState(jsonTypeInfo); |
| | 0 | 172 | | return base.OnTryRead(ref reader, typeToConvert, options, ref state, out value); |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | // Handle metadata post polymorphic dispatch |
| | 8 | 176 | | if (state.Current.ObjectState < StackFrameObjectState.ConstructorArguments) |
| | 8 | 177 | | { |
| | 8 | 178 | | if (state.Current.CanContainMetadata) |
| | 0 | 179 | | { |
| | 0 | 180 | | JsonSerializer.ValidateMetadataForObjectConverter(ref state); |
| | 0 | 181 | | } |
| | | 182 | | |
| | 8 | 183 | | if (state.Current.MetadataPropertyNames == MetadataPropertyName.Ref) |
| | 0 | 184 | | { |
| | 0 | 185 | | value = JsonSerializer.ResolveReferenceId<T>(ref state); |
| | 0 | 186 | | return true; |
| | | 187 | | } |
| | | 188 | | |
| | 8 | 189 | | BeginRead(ref state, options); |
| | | 190 | | |
| | 8 | 191 | | state.Current.ObjectState = StackFrameObjectState.ConstructorArguments; |
| | 8 | 192 | | } |
| | | 193 | | |
| | 8 | 194 | | if (!ReadConstructorArgumentsWithContinuation(ref state, ref reader, options)) |
| | 0 | 195 | | { |
| | 0 | 196 | | value = default; |
| | 0 | 197 | | return false; |
| | | 198 | | } |
| | | 199 | | |
| | | 200 | | // We've read all ctor parameters and properties, |
| | | 201 | | // validate that all required parameters were provided |
| | | 202 | | // before calling the constructor which may throw. |
| | 0 | 203 | | state.Current.ValidateAllRequiredPropertiesAreRead(jsonTypeInfo); |
| | | 204 | | |
| | 0 | 205 | | obj = (T)CreateObject(ref state.Current); |
| | | 206 | | |
| | 0 | 207 | | if ((state.Current.MetadataPropertyNames & MetadataPropertyName.Id) != 0) |
| | 0 | 208 | | { |
| | 0 | 209 | | Debug.Assert(state.ReferenceId != null); |
| | 0 | 210 | | Debug.Assert(options.ReferenceHandlingStrategy == JsonKnownReferenceHandler.Preserve); |
| | 0 | 211 | | state.ReferenceResolver.AddReference(state.ReferenceId, obj); |
| | 0 | 212 | | state.ReferenceId = null; |
| | 0 | 213 | | } |
| | | 214 | | |
| | 0 | 215 | | jsonTypeInfo.OnDeserializing?.Invoke(obj); |
| | | 216 | | |
| | 0 | 217 | | if (argumentState.FoundPropertyCount > 0) |
| | 0 | 218 | | { |
| | 0 | 219 | | for (int i = 0; i < argumentState.FoundPropertyCount; i++) |
| | 0 | 220 | | { |
| | 0 | 221 | | JsonPropertyInfo jsonPropertyInfo = argumentState.FoundPropertiesAsync![i].Item1; |
| | 0 | 222 | | object? propValue = argumentState.FoundPropertiesAsync![i].Item2; |
| | 0 | 223 | | string? dataExtKey = argumentState.FoundPropertiesAsync![i].Item3; |
| | | 224 | | |
| | 0 | 225 | | if (dataExtKey == null) |
| | 0 | 226 | | { |
| | 0 | 227 | | Debug.Assert(jsonPropertyInfo.Set != null); |
| | | 228 | | |
| | 0 | 229 | | if (propValue is not null || !jsonPropertyInfo.IgnoreNullTokensOnRead || default(T) is not n |
| | 0 | 230 | | { |
| | 0 | 231 | | jsonPropertyInfo.Set(obj, propValue); |
| | 0 | 232 | | } |
| | 0 | 233 | | } |
| | | 234 | | else |
| | 0 | 235 | | { |
| | 0 | 236 | | Debug.Assert(jsonPropertyInfo == state.Current.JsonTypeInfo.ExtensionDataProperty); |
| | | 237 | | |
| | 0 | 238 | | JsonSerializer.CreateExtensionDataProperty(obj, jsonPropertyInfo, options); |
| | 0 | 239 | | object extDictionary = jsonPropertyInfo.GetValueAsObject(obj)!; |
| | | 240 | | |
| | 0 | 241 | | if (extDictionary is IDictionary<string, JsonElement> dict) |
| | 0 | 242 | | { |
| | 0 | 243 | | if (options.AllowDuplicateProperties) |
| | 0 | 244 | | { |
| | 0 | 245 | | dict[dataExtKey] = (JsonElement)propValue!; |
| | 0 | 246 | | } |
| | 0 | 247 | | else if (!dict.TryAdd(dataExtKey, (JsonElement)propValue!)) |
| | 0 | 248 | | { |
| | 0 | 249 | | ThrowHelper.ThrowJsonException_DuplicatePropertyNotAllowed(dataExtKey); |
| | | 250 | | } |
| | 0 | 251 | | } |
| | | 252 | | else |
| | 0 | 253 | | { |
| | 0 | 254 | | IDictionary<string, object> objDict = (IDictionary<string, object>)extDictionary; |
| | | 255 | | |
| | 0 | 256 | | if (options.AllowDuplicateProperties) |
| | 0 | 257 | | { |
| | 0 | 258 | | objDict[dataExtKey] = propValue!; |
| | 0 | 259 | | } |
| | 0 | 260 | | else if (!objDict.TryAdd(dataExtKey, propValue!)) |
| | 0 | 261 | | { |
| | 0 | 262 | | ThrowHelper.ThrowJsonException_DuplicatePropertyNotAllowed(dataExtKey); |
| | | 263 | | } |
| | 0 | 264 | | } |
| | 0 | 265 | | } |
| | 0 | 266 | | } |
| | | 267 | | |
| | 0 | 268 | | FoundPropertyAsync[] toReturn = argumentState.FoundPropertiesAsync!; |
| | 0 | 269 | | argumentState.FoundPropertiesAsync = null; |
| | 0 | 270 | | ArrayPool<FoundPropertyAsync>.Shared.Return(toReturn, clearArray: true); |
| | 0 | 271 | | } |
| | 0 | 272 | | } |
| | | 273 | | |
| | 0 | 274 | | jsonTypeInfo.OnDeserialized?.Invoke(obj); |
| | | 275 | | |
| | | 276 | | // Unbox |
| | 0 | 277 | | Debug.Assert(obj != null); |
| | 0 | 278 | | value = (T)obj; |
| | | 279 | | |
| | | 280 | | // Check if we are trying to update the UTF-8 property cache. |
| | 0 | 281 | | if (state.Current.PropertyRefCacheBuilder != null) |
| | 0 | 282 | | { |
| | 0 | 283 | | jsonTypeInfo.UpdateUtf8PropertyCache(ref state.Current); |
| | 0 | 284 | | } |
| | | 285 | | |
| | 0 | 286 | | return true; |
| | 0 | 287 | | } |
| | | 288 | | |
| | | 289 | | protected abstract void InitializeConstructorArgumentCaches(ref ReadStack state, JsonSerializerOptions options); |
| | | 290 | | |
| | | 291 | | protected abstract bool ReadAndCacheConstructorArgument(scoped ref ReadStack state, ref Utf8JsonReader reader, J |
| | | 292 | | |
| | | 293 | | protected abstract object CreateObject(ref ReadStackFrame frame); |
| | | 294 | | |
| | | 295 | | /// <summary> |
| | | 296 | | /// Performs a full first pass of the JSON input and deserializes the ctor args. |
| | | 297 | | /// </summary> |
| | | 298 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 299 | | private void ReadConstructorArguments(scoped ref ReadStack state, ref Utf8JsonReader reader, JsonSerializerOptio |
| | 0 | 300 | | { |
| | 0 | 301 | | BeginRead(ref state, options); |
| | | 302 | | |
| | 0 | 303 | | while (true) |
| | 0 | 304 | | { |
| | | 305 | | // Read the next property name or EndObject. |
| | 0 | 306 | | reader.ReadWithVerify(); |
| | | 307 | | |
| | 0 | 308 | | JsonTokenType tokenType = reader.TokenType; |
| | | 309 | | |
| | 0 | 310 | | if (tokenType == JsonTokenType.EndObject) |
| | 0 | 311 | | { |
| | 0 | 312 | | return; |
| | | 313 | | } |
| | | 314 | | |
| | | 315 | | // Read method would have thrown if otherwise. |
| | 0 | 316 | | Debug.Assert(tokenType == JsonTokenType.PropertyName); |
| | | 317 | | |
| | 0 | 318 | | ReadOnlySpan<byte> unescapedPropertyName = JsonSerializer.GetPropertyName(ref state, ref reader, options |
| | 0 | 319 | | if (isAlreadyReadMetadataProperty) |
| | 0 | 320 | | { |
| | 0 | 321 | | Debug.Assert(options.AllowOutOfOrderMetadataProperties); |
| | 0 | 322 | | reader.SkipWithVerify(); |
| | 0 | 323 | | state.Current.EndProperty(); |
| | 0 | 324 | | continue; |
| | | 325 | | } |
| | | 326 | | |
| | 0 | 327 | | if (TryLookupConstructorParameter( |
| | 0 | 328 | | unescapedPropertyName, |
| | 0 | 329 | | ref state, |
| | 0 | 330 | | options, |
| | 0 | 331 | | out JsonPropertyInfo jsonPropertyInfo, |
| | 0 | 332 | | out JsonParameterInfo? jsonParameterInfo)) |
| | 0 | 333 | | { |
| | | 334 | | // Set the property value. |
| | 0 | 335 | | reader.ReadWithVerify(); |
| | | 336 | | |
| | 0 | 337 | | if (!jsonParameterInfo.ShouldDeserialize) |
| | 0 | 338 | | { |
| | | 339 | | // The Utf8JsonReader.Skip() method will fail fast if it detects that we're reading |
| | | 340 | | // from a partially read buffer, regardless of whether the next value is available. |
| | | 341 | | // This can result in erroneous failures in cases where a custom converter is calling |
| | | 342 | | // into a built-in converter (cf. https://github.com/dotnet/runtime/issues/74108). |
| | | 343 | | // For this reason we need to call the TrySkip() method instead -- the serializer |
| | | 344 | | // should guarantee sufficient read-ahead has been performed for the current object. |
| | 0 | 345 | | bool success = reader.TrySkip(); |
| | 0 | 346 | | Debug.Assert(success, "Serializer should guarantee sufficient read-ahead has been done."); |
| | | 347 | | |
| | 0 | 348 | | state.Current.EndConstructorParameter(); |
| | 0 | 349 | | continue; |
| | | 350 | | } |
| | | 351 | | |
| | 0 | 352 | | Debug.Assert(jsonParameterInfo.MatchingProperty != null); |
| | 0 | 353 | | ReadAndCacheConstructorArgument(ref state, ref reader, jsonParameterInfo); |
| | | 354 | | |
| | 0 | 355 | | state.Current.EndConstructorParameter(); |
| | 0 | 356 | | } |
| | | 357 | | else |
| | 0 | 358 | | { |
| | 0 | 359 | | if (jsonPropertyInfo.CanDeserialize) |
| | 0 | 360 | | { |
| | 0 | 361 | | ArgumentState argumentState = state.Current.CtorArgumentState!; |
| | | 362 | | |
| | 0 | 363 | | if (argumentState.FoundProperties == null) |
| | 0 | 364 | | { |
| | 0 | 365 | | argumentState.FoundProperties = |
| | 0 | 366 | | ArrayPool<FoundProperty>.Shared.Rent(Math.Max(1, state.Current.JsonTypeInfo.PropertyCach |
| | 0 | 367 | | } |
| | 0 | 368 | | else if (argumentState.FoundPropertyCount == argumentState.FoundProperties.Length) |
| | 0 | 369 | | { |
| | | 370 | | // Rare case where we can't fit all the JSON properties in the rented pool; we have to grow. |
| | | 371 | | // This could happen if there are duplicate properties in the JSON. |
| | | 372 | | |
| | 0 | 373 | | var newCache = ArrayPool<FoundProperty>.Shared.Rent(argumentState.FoundProperties.Length * 2 |
| | | 374 | | |
| | 0 | 375 | | argumentState.FoundProperties.CopyTo(newCache, 0); |
| | | 376 | | |
| | 0 | 377 | | FoundProperty[] toReturn = argumentState.FoundProperties; |
| | 0 | 378 | | argumentState.FoundProperties = newCache!; |
| | | 379 | | |
| | 0 | 380 | | ArrayPool<FoundProperty>.Shared.Return(toReturn, clearArray: true); |
| | 0 | 381 | | } |
| | | 382 | | |
| | 0 | 383 | | argumentState.FoundProperties[argumentState.FoundPropertyCount++] = ( |
| | 0 | 384 | | jsonPropertyInfo, |
| | 0 | 385 | | reader.CurrentState, |
| | 0 | 386 | | reader.BytesConsumed, |
| | 0 | 387 | | state.Current.JsonPropertyName, |
| | 0 | 388 | | state.Current.JsonPropertyNameAsString); |
| | 0 | 389 | | } |
| | | 390 | | |
| | 0 | 391 | | reader.SkipWithVerify(); |
| | 0 | 392 | | state.Current.EndProperty(); |
| | 0 | 393 | | } |
| | 0 | 394 | | } |
| | 0 | 395 | | } |
| | | 396 | | |
| | | 397 | | private bool ReadConstructorArgumentsWithContinuation(scoped ref ReadStack state, ref Utf8JsonReader reader, Jso |
| | 8 | 398 | | { |
| | | 399 | | // Process all properties. |
| | 8 | 400 | | while (true) |
| | 8 | 401 | | { |
| | | 402 | | // Determine the property. |
| | 8 | 403 | | if (state.Current.PropertyState == StackFramePropertyState.None) |
| | 8 | 404 | | { |
| | 8 | 405 | | if (!reader.Read()) |
| | 0 | 406 | | { |
| | 0 | 407 | | return false; |
| | | 408 | | } |
| | | 409 | | |
| | 0 | 410 | | state.Current.PropertyState = StackFramePropertyState.ReadName; |
| | 0 | 411 | | } |
| | | 412 | | |
| | | 413 | | JsonParameterInfo? jsonParameterInfo; |
| | | 414 | | JsonPropertyInfo? jsonPropertyInfo; |
| | | 415 | | |
| | 0 | 416 | | if (state.Current.PropertyState < StackFramePropertyState.Name) |
| | 0 | 417 | | { |
| | 0 | 418 | | JsonTokenType tokenType = reader.TokenType; |
| | | 419 | | |
| | 0 | 420 | | if (tokenType == JsonTokenType.EndObject) |
| | 0 | 421 | | { |
| | 0 | 422 | | return true; |
| | | 423 | | } |
| | | 424 | | |
| | | 425 | | // Read method would have thrown if otherwise. |
| | 0 | 426 | | Debug.Assert(tokenType == JsonTokenType.PropertyName); |
| | | 427 | | |
| | 0 | 428 | | ReadOnlySpan<byte> unescapedPropertyName = JsonSerializer.GetPropertyName(ref state, ref reader, opt |
| | 0 | 429 | | if (isAlreadyReadMetadataProperty) |
| | 0 | 430 | | { |
| | 0 | 431 | | Debug.Assert(options.AllowOutOfOrderMetadataProperties); |
| | 0 | 432 | | reader.SkipWithVerify(); |
| | 0 | 433 | | state.Current.EndProperty(); |
| | 0 | 434 | | continue; |
| | | 435 | | } |
| | | 436 | | |
| | 0 | 437 | | if (TryLookupConstructorParameter( |
| | 0 | 438 | | unescapedPropertyName, |
| | 0 | 439 | | ref state, |
| | 0 | 440 | | options, |
| | 0 | 441 | | out jsonPropertyInfo, |
| | 0 | 442 | | out jsonParameterInfo)) |
| | 0 | 443 | | { |
| | 0 | 444 | | jsonPropertyInfo = null; |
| | 0 | 445 | | } |
| | | 446 | | |
| | 0 | 447 | | state.Current.PropertyState = StackFramePropertyState.Name; |
| | 0 | 448 | | } |
| | | 449 | | else |
| | 0 | 450 | | { |
| | 0 | 451 | | jsonParameterInfo = state.Current.CtorArgumentState!.JsonParameterInfo; |
| | 0 | 452 | | jsonPropertyInfo = state.Current.JsonPropertyInfo; |
| | 0 | 453 | | } |
| | | 454 | | |
| | 0 | 455 | | if (jsonParameterInfo != null) |
| | 0 | 456 | | { |
| | 0 | 457 | | Debug.Assert(jsonPropertyInfo == null); |
| | | 458 | | |
| | 0 | 459 | | if (!HandleConstructorArgumentWithContinuation(ref state, ref reader, jsonParameterInfo)) |
| | 0 | 460 | | { |
| | 0 | 461 | | return false; |
| | | 462 | | } |
| | 0 | 463 | | } |
| | | 464 | | else |
| | 0 | 465 | | { |
| | 0 | 466 | | if (!HandlePropertyWithContinuation(ref state, ref reader, jsonPropertyInfo!)) |
| | 0 | 467 | | { |
| | 0 | 468 | | return false; |
| | | 469 | | } |
| | 0 | 470 | | } |
| | 0 | 471 | | } |
| | 0 | 472 | | } |
| | | 473 | | |
| | | 474 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 475 | | private bool HandleConstructorArgumentWithContinuation( |
| | | 476 | | scoped ref ReadStack state, |
| | | 477 | | ref Utf8JsonReader reader, |
| | | 478 | | JsonParameterInfo jsonParameterInfo) |
| | 0 | 479 | | { |
| | 0 | 480 | | if (state.Current.PropertyState < StackFramePropertyState.ReadValue) |
| | 0 | 481 | | { |
| | 0 | 482 | | if (!jsonParameterInfo.ShouldDeserialize) |
| | 0 | 483 | | { |
| | 0 | 484 | | if (!reader.TrySkipPartial(targetDepth: state.Current.OriginalDepth + 1)) |
| | 0 | 485 | | { |
| | 0 | 486 | | return false; |
| | | 487 | | } |
| | | 488 | | |
| | 0 | 489 | | state.Current.EndConstructorParameter(); |
| | 0 | 490 | | return true; |
| | | 491 | | } |
| | | 492 | | |
| | 0 | 493 | | if (!reader.TryAdvanceWithOptionalReadAhead(jsonParameterInfo.EffectiveConverter.RequiresReadAhead)) |
| | 0 | 494 | | { |
| | 0 | 495 | | return false; |
| | | 496 | | } |
| | | 497 | | |
| | 0 | 498 | | state.Current.PropertyState = StackFramePropertyState.ReadValue; |
| | 0 | 499 | | } |
| | | 500 | | |
| | 0 | 501 | | if (!ReadAndCacheConstructorArgument(ref state, ref reader, jsonParameterInfo)) |
| | 0 | 502 | | { |
| | 0 | 503 | | return false; |
| | | 504 | | } |
| | | 505 | | |
| | 0 | 506 | | state.Current.EndConstructorParameter(); |
| | 0 | 507 | | return true; |
| | 0 | 508 | | } |
| | | 509 | | |
| | | 510 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 511 | | private static bool HandlePropertyWithContinuation( |
| | | 512 | | scoped ref ReadStack state, |
| | | 513 | | ref Utf8JsonReader reader, |
| | | 514 | | JsonPropertyInfo jsonPropertyInfo) |
| | 0 | 515 | | { |
| | 0 | 516 | | if (state.Current.PropertyState < StackFramePropertyState.ReadValue) |
| | 0 | 517 | | { |
| | 0 | 518 | | if (!jsonPropertyInfo.CanDeserialize) |
| | 0 | 519 | | { |
| | 0 | 520 | | if (!reader.TrySkipPartial(targetDepth: state.Current.OriginalDepth + 1)) |
| | 0 | 521 | | { |
| | 0 | 522 | | return false; |
| | | 523 | | } |
| | | 524 | | |
| | 0 | 525 | | state.Current.EndProperty(); |
| | 0 | 526 | | return true; |
| | | 527 | | } |
| | | 528 | | |
| | 0 | 529 | | if (!ReadAheadPropertyValue(ref state, ref reader, jsonPropertyInfo)) |
| | 0 | 530 | | { |
| | 0 | 531 | | return false; |
| | | 532 | | } |
| | | 533 | | |
| | 0 | 534 | | state.Current.PropertyState = StackFramePropertyState.ReadValue; |
| | 0 | 535 | | } |
| | | 536 | | |
| | | 537 | | object? propValue; |
| | | 538 | | |
| | 0 | 539 | | if (state.Current.UseExtensionProperty) |
| | 0 | 540 | | { |
| | 0 | 541 | | if (!jsonPropertyInfo.ReadJsonExtensionDataValue(ref state, ref reader, out propValue)) |
| | 0 | 542 | | { |
| | 0 | 543 | | return false; |
| | | 544 | | } |
| | 0 | 545 | | } |
| | | 546 | | else |
| | 0 | 547 | | { |
| | 0 | 548 | | if (!jsonPropertyInfo.ReadJsonAsObject(ref state, ref reader, out propValue)) |
| | 0 | 549 | | { |
| | 0 | 550 | | return false; |
| | | 551 | | } |
| | 0 | 552 | | } |
| | | 553 | | |
| | 0 | 554 | | Debug.Assert(jsonPropertyInfo.CanDeserialize); |
| | | 555 | | |
| | | 556 | | // Ensure that the cache has enough capacity to add this property. |
| | | 557 | | |
| | 0 | 558 | | ArgumentState argumentState = state.Current.CtorArgumentState!; |
| | | 559 | | |
| | 0 | 560 | | if (argumentState.FoundPropertiesAsync == null) |
| | 0 | 561 | | { |
| | 0 | 562 | | argumentState.FoundPropertiesAsync = ArrayPool<FoundPropertyAsync>.Shared.Rent(Math.Max(1, state.Current |
| | 0 | 563 | | } |
| | 0 | 564 | | else if (argumentState.FoundPropertyCount == argumentState.FoundPropertiesAsync!.Length) |
| | 0 | 565 | | { |
| | | 566 | | // Rare case where we can't fit all the JSON properties in the rented pool; we have to grow. |
| | | 567 | | // This could happen if there are duplicate properties in the JSON. |
| | 0 | 568 | | var newCache = ArrayPool<FoundPropertyAsync>.Shared.Rent(argumentState.FoundPropertiesAsync!.Length * 2) |
| | | 569 | | |
| | 0 | 570 | | argumentState.FoundPropertiesAsync!.CopyTo(newCache, 0); |
| | | 571 | | |
| | 0 | 572 | | FoundPropertyAsync[] toReturn = argumentState.FoundPropertiesAsync!; |
| | 0 | 573 | | argumentState.FoundPropertiesAsync = newCache!; |
| | | 574 | | |
| | 0 | 575 | | ArrayPool<FoundPropertyAsync>.Shared.Return(toReturn, clearArray: true); |
| | 0 | 576 | | } |
| | | 577 | | |
| | | 578 | | // Cache the property name and value. |
| | 0 | 579 | | argumentState.FoundPropertiesAsync![argumentState.FoundPropertyCount++] = ( |
| | 0 | 580 | | jsonPropertyInfo, |
| | 0 | 581 | | propValue, |
| | 0 | 582 | | state.Current.JsonPropertyNameAsString); |
| | | 583 | | |
| | 0 | 584 | | state.Current.EndProperty(); |
| | 0 | 585 | | return true; |
| | 0 | 586 | | } |
| | | 587 | | |
| | | 588 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 589 | | private void BeginRead(scoped ref ReadStack state, JsonSerializerOptions options) |
| | 8 | 590 | | { |
| | 8 | 591 | | JsonTypeInfo jsonTypeInfo = state.Current.JsonTypeInfo; |
| | | 592 | | |
| | 8 | 593 | | jsonTypeInfo.ValidateCanBeUsedForPropertyMetadataSerialization(); |
| | | 594 | | |
| | 8 | 595 | | if (jsonTypeInfo.ParameterCount != jsonTypeInfo.ParameterCache.Length) |
| | 0 | 596 | | { |
| | 0 | 597 | | ThrowHelper.ThrowInvalidOperationException_ConstructorParameterIncompleteBinding(Type); |
| | | 598 | | } |
| | | 599 | | |
| | 8 | 600 | | state.Current.InitializePropertiesValidationState(jsonTypeInfo); |
| | | 601 | | |
| | | 602 | | // Set current JsonPropertyInfo to null to avoid conflicts on push. |
| | 8 | 603 | | state.Current.JsonPropertyInfo = null; |
| | | 604 | | |
| | 8 | 605 | | Debug.Assert(state.Current.CtorArgumentState != null); |
| | | 606 | | |
| | 8 | 607 | | InitializeConstructorArgumentCaches(ref state, options); |
| | 8 | 608 | | } |
| | | 609 | | |
| | | 610 | | /// <summary> |
| | | 611 | | /// Lookup the constructor parameter given its name in the reader. |
| | | 612 | | /// </summary> |
| | | 613 | | protected static bool TryLookupConstructorParameter( |
| | | 614 | | scoped ReadOnlySpan<byte> unescapedPropertyName, |
| | | 615 | | scoped ref ReadStack state, |
| | | 616 | | JsonSerializerOptions options, |
| | | 617 | | out JsonPropertyInfo jsonPropertyInfo, |
| | | 618 | | [NotNullWhen(true)] out JsonParameterInfo? jsonParameterInfo) |
| | 0 | 619 | | { |
| | 0 | 620 | | Debug.Assert(state.Current.JsonTypeInfo.Kind is JsonTypeInfoKind.Object); |
| | 0 | 621 | | Debug.Assert(state.Current.CtorArgumentState != null); |
| | | 622 | | |
| | 0 | 623 | | jsonPropertyInfo = JsonSerializer.LookupProperty( |
| | 0 | 624 | | obj: null, |
| | 0 | 625 | | unescapedPropertyName, |
| | 0 | 626 | | ref state, |
| | 0 | 627 | | options, |
| | 0 | 628 | | out bool useExtensionProperty, |
| | 0 | 629 | | createExtensionProperty: false); |
| | | 630 | | |
| | | 631 | | // Mark the property as read from the payload if it is mapped to a non-extension member. |
| | 0 | 632 | | if (!useExtensionProperty && jsonPropertyInfo != JsonPropertyInfo.s_missingProperty) |
| | 0 | 633 | | { |
| | 0 | 634 | | state.Current.MarkPropertyAsRead(jsonPropertyInfo); |
| | 0 | 635 | | } |
| | | 636 | | |
| | 0 | 637 | | jsonParameterInfo = jsonPropertyInfo.AssociatedParameter; |
| | 0 | 638 | | if (jsonParameterInfo != null) |
| | 0 | 639 | | { |
| | 0 | 640 | | state.Current.JsonPropertyInfo = null; |
| | 0 | 641 | | state.Current.CtorArgumentState!.JsonParameterInfo = jsonParameterInfo; |
| | 0 | 642 | | state.Current.NumberHandling = jsonParameterInfo.NumberHandling; |
| | 0 | 643 | | return true; |
| | | 644 | | } |
| | | 645 | | else |
| | 0 | 646 | | { |
| | 0 | 647 | | state.Current.UseExtensionProperty = useExtensionProperty; |
| | 0 | 648 | | return false; |
| | | 649 | | } |
| | 0 | 650 | | } |
| | | 651 | | } |
| | | 652 | | } |