| | | 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.Generic; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.Runtime.CompilerServices; |
| | | 8 | | using System.Text.Json.Serialization.Metadata; |
| | | 9 | | |
| | | 10 | | namespace System.Text.Json.Serialization.Converters |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Default base class implementation of <cref>JsonObjectConverter{T}</cref>. |
| | | 14 | | /// </summary> |
| | | 15 | | internal class ObjectDefaultConverter<T> : JsonObjectConverter<T> where T : notnull |
| | | 16 | | { |
| | 0 | 17 | | internal override bool CanHaveMetadata => true; |
| | 1849 | 18 | | internal override bool SupportsCreateObjectDelegate => true; |
| | | 19 | | |
| | | 20 | | internal override bool OnTryRead(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options, s |
| | 140 | 21 | | { |
| | 140 | 22 | | JsonTypeInfo jsonTypeInfo = state.Current.JsonTypeInfo; |
| | | 23 | | |
| | | 24 | | object obj; |
| | | 25 | | |
| | 140 | 26 | | if (!state.SupportContinuation && !state.Current.CanContainMetadata) |
| | 0 | 27 | | { |
| | | 28 | | // Fast path that avoids maintaining state variables and dealing with preserved references. |
| | | 29 | | |
| | 0 | 30 | | if (reader.TokenType != JsonTokenType.StartObject) |
| | 0 | 31 | | { |
| | 0 | 32 | | ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type); |
| | | 33 | | } |
| | | 34 | | |
| | 0 | 35 | | if (state.ParentProperty?.TryGetPrePopulatedValue(ref state) == true) |
| | 0 | 36 | | { |
| | 0 | 37 | | obj = state.Current.ReturnValue!; |
| | 0 | 38 | | } |
| | | 39 | | else |
| | 0 | 40 | | { |
| | 0 | 41 | | if (jsonTypeInfo.CreateObject == null) |
| | 0 | 42 | | { |
| | 0 | 43 | | ThrowHelper.ThrowNotSupportedException_DeserializeNoConstructor(jsonTypeInfo, ref reader, ref st |
| | | 44 | | } |
| | | 45 | | |
| | 0 | 46 | | obj = jsonTypeInfo.CreateObject(); |
| | 0 | 47 | | } |
| | | 48 | | |
| | 0 | 49 | | PopulatePropertiesFastPath(obj, jsonTypeInfo, options, ref reader, ref state); |
| | 0 | 50 | | Debug.Assert(obj != null); |
| | 0 | 51 | | value = (T)obj; |
| | 0 | 52 | | return true; |
| | | 53 | | } |
| | | 54 | | else |
| | 140 | 55 | | { |
| | | 56 | | // Slower path that supports continuation and reading metadata. |
| | | 57 | | |
| | 140 | 58 | | if (state.Current.ObjectState == StackFrameObjectState.None) |
| | 140 | 59 | | { |
| | 140 | 60 | | if (reader.TokenType != JsonTokenType.StartObject) |
| | 118 | 61 | | { |
| | 118 | 62 | | ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type); |
| | | 63 | | } |
| | | 64 | | |
| | 22 | 65 | | state.Current.ObjectState = StackFrameObjectState.StartToken; |
| | 22 | 66 | | } |
| | | 67 | | |
| | | 68 | | // Handle the metadata properties. |
| | 22 | 69 | | if (state.Current.CanContainMetadata && state.Current.ObjectState < StackFrameObjectState.ReadMetadata) |
| | 0 | 70 | | { |
| | 0 | 71 | | if (!JsonSerializer.TryReadMetadata(this, jsonTypeInfo, ref reader, ref state)) |
| | 0 | 72 | | { |
| | 0 | 73 | | value = default; |
| | 0 | 74 | | return false; |
| | | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | if (state.Current.MetadataPropertyNames == MetadataPropertyName.Ref) |
| | 0 | 78 | | { |
| | 0 | 79 | | value = JsonSerializer.ResolveReferenceId<T>(ref state); |
| | 0 | 80 | | return true; |
| | | 81 | | } |
| | | 82 | | |
| | 0 | 83 | | state.Current.ObjectState = StackFrameObjectState.ReadMetadata; |
| | 0 | 84 | | } |
| | | 85 | | |
| | | 86 | | // Dispatch to any polymorphic converters: should always be entered regardless of ObjectState progress |
| | 22 | 87 | | if ((state.Current.MetadataPropertyNames & MetadataPropertyName.Type) != 0 && |
| | 22 | 88 | | state.Current.PolymorphicSerializationState != PolymorphicSerializationState.PolymorphicReEntryStart |
| | 22 | 89 | | ResolvePolymorphicConverter(jsonTypeInfo, ref state) is JsonConverter polymorphicConverter) |
| | 0 | 90 | | { |
| | 0 | 91 | | Debug.Assert(!IsValueType); |
| | 0 | 92 | | bool success = polymorphicConverter.OnTryReadAsObject(ref reader, polymorphicConverter.Type!, option |
| | 0 | 93 | | value = (T)objectResult!; |
| | 0 | 94 | | state.ExitPolymorphicConverter(success); |
| | 0 | 95 | | return success; |
| | | 96 | | } |
| | | 97 | | |
| | 22 | 98 | | if (state.Current.ObjectState < StackFrameObjectState.CreatedObject) |
| | 22 | 99 | | { |
| | 22 | 100 | | if (state.Current.CanContainMetadata) |
| | 0 | 101 | | { |
| | 0 | 102 | | JsonSerializer.ValidateMetadataForObjectConverter(ref state); |
| | 0 | 103 | | } |
| | | 104 | | |
| | 22 | 105 | | if (state.Current.MetadataPropertyNames == MetadataPropertyName.Ref) |
| | 0 | 106 | | { |
| | 0 | 107 | | value = JsonSerializer.ResolveReferenceId<T>(ref state); |
| | 0 | 108 | | return true; |
| | | 109 | | } |
| | | 110 | | |
| | 22 | 111 | | if (state.ParentProperty?.TryGetPrePopulatedValue(ref state) == true) |
| | 0 | 112 | | { |
| | 0 | 113 | | obj = state.Current.ReturnValue!; |
| | 0 | 114 | | } |
| | | 115 | | else |
| | 22 | 116 | | { |
| | 22 | 117 | | if (jsonTypeInfo.CreateObject == null) |
| | 0 | 118 | | { |
| | 0 | 119 | | ThrowHelper.ThrowNotSupportedException_DeserializeNoConstructor(jsonTypeInfo, ref reader, re |
| | | 120 | | } |
| | | 121 | | |
| | 22 | 122 | | obj = jsonTypeInfo.CreateObject(); |
| | 22 | 123 | | } |
| | | 124 | | |
| | 22 | 125 | | if ((state.Current.MetadataPropertyNames & MetadataPropertyName.Id) != 0) |
| | 0 | 126 | | { |
| | 0 | 127 | | Debug.Assert(state.ReferenceId != null); |
| | 0 | 128 | | Debug.Assert(options.ReferenceHandlingStrategy == JsonKnownReferenceHandler.Preserve); |
| | 0 | 129 | | state.ReferenceResolver.AddReference(state.ReferenceId, obj); |
| | 0 | 130 | | state.ReferenceId = null; |
| | 0 | 131 | | } |
| | | 132 | | |
| | 22 | 133 | | jsonTypeInfo.OnDeserializing?.Invoke(obj); |
| | | 134 | | |
| | 22 | 135 | | state.Current.ReturnValue = obj; |
| | 22 | 136 | | state.Current.ObjectState = StackFrameObjectState.CreatedObject; |
| | 22 | 137 | | state.Current.InitializePropertiesValidationState(jsonTypeInfo); |
| | 22 | 138 | | } |
| | | 139 | | else |
| | 0 | 140 | | { |
| | 0 | 141 | | obj = state.Current.ReturnValue!; |
| | 0 | 142 | | Debug.Assert(obj != null); |
| | 0 | 143 | | } |
| | | 144 | | |
| | | 145 | | // Process all properties. |
| | 22 | 146 | | while (true) |
| | 22 | 147 | | { |
| | | 148 | | // Determine the property. |
| | 22 | 149 | | if (state.Current.PropertyState == StackFramePropertyState.None) |
| | 22 | 150 | | { |
| | 22 | 151 | | if (!reader.Read()) |
| | 0 | 152 | | { |
| | 0 | 153 | | state.Current.ReturnValue = obj; |
| | 0 | 154 | | value = default; |
| | 0 | 155 | | return false; |
| | | 156 | | } |
| | | 157 | | |
| | 0 | 158 | | state.Current.PropertyState = StackFramePropertyState.ReadName; |
| | 0 | 159 | | } |
| | | 160 | | |
| | | 161 | | JsonPropertyInfo jsonPropertyInfo; |
| | | 162 | | |
| | 0 | 163 | | if (state.Current.PropertyState < StackFramePropertyState.Name) |
| | 0 | 164 | | { |
| | 0 | 165 | | JsonTokenType tokenType = reader.TokenType; |
| | 0 | 166 | | if (tokenType == JsonTokenType.EndObject) |
| | 0 | 167 | | { |
| | 0 | 168 | | break; |
| | | 169 | | } |
| | | 170 | | |
| | | 171 | | // Read method would have thrown if otherwise. |
| | 0 | 172 | | Debug.Assert(tokenType == JsonTokenType.PropertyName); |
| | | 173 | | |
| | 0 | 174 | | jsonTypeInfo.ValidateCanBeUsedForPropertyMetadataSerialization(); |
| | 0 | 175 | | ReadOnlySpan<byte> unescapedPropertyName = JsonSerializer.GetPropertyName(ref state, ref reader, |
| | 0 | 176 | | if (isAlreadyReadMetadataProperty) |
| | 0 | 177 | | { |
| | 0 | 178 | | Debug.Assert(options.AllowOutOfOrderMetadataProperties); |
| | 0 | 179 | | reader.SkipWithVerify(); |
| | 0 | 180 | | state.Current.EndProperty(); |
| | 0 | 181 | | continue; |
| | | 182 | | } |
| | | 183 | | |
| | 0 | 184 | | jsonPropertyInfo = JsonSerializer.LookupProperty( |
| | 0 | 185 | | obj, |
| | 0 | 186 | | unescapedPropertyName, |
| | 0 | 187 | | ref state, |
| | 0 | 188 | | options, |
| | 0 | 189 | | out bool useExtensionProperty); |
| | | 190 | | |
| | 0 | 191 | | state.Current.UseExtensionProperty = useExtensionProperty; |
| | 0 | 192 | | state.Current.PropertyState = StackFramePropertyState.Name; |
| | 0 | 193 | | } |
| | | 194 | | else |
| | 0 | 195 | | { |
| | 0 | 196 | | Debug.Assert(state.Current.JsonPropertyInfo != null); |
| | 0 | 197 | | jsonPropertyInfo = state.Current.JsonPropertyInfo!; |
| | 0 | 198 | | } |
| | | 199 | | |
| | 0 | 200 | | if (state.Current.PropertyState < StackFramePropertyState.ReadValue) |
| | 0 | 201 | | { |
| | 0 | 202 | | if (!jsonPropertyInfo.CanDeserializeOrPopulate) |
| | 0 | 203 | | { |
| | 0 | 204 | | if (!reader.TrySkipPartial(targetDepth: state.Current.OriginalDepth + 1)) |
| | 0 | 205 | | { |
| | 0 | 206 | | state.Current.ReturnValue = obj; |
| | 0 | 207 | | value = default; |
| | 0 | 208 | | return false; |
| | | 209 | | } |
| | | 210 | | |
| | 0 | 211 | | state.Current.EndProperty(); |
| | 0 | 212 | | continue; |
| | | 213 | | } |
| | | 214 | | |
| | 0 | 215 | | if (!ReadAheadPropertyValue(ref state, ref reader, jsonPropertyInfo)) |
| | 0 | 216 | | { |
| | 0 | 217 | | state.Current.ReturnValue = obj; |
| | 0 | 218 | | value = default; |
| | 0 | 219 | | return false; |
| | | 220 | | } |
| | | 221 | | |
| | 0 | 222 | | state.Current.PropertyState = StackFramePropertyState.ReadValue; |
| | 0 | 223 | | } |
| | | 224 | | |
| | 0 | 225 | | if (state.Current.PropertyState < StackFramePropertyState.TryRead) |
| | 0 | 226 | | { |
| | | 227 | | // Obtain the CLR value from the JSON and set the member. |
| | 0 | 228 | | if (!state.Current.UseExtensionProperty) |
| | 0 | 229 | | { |
| | 0 | 230 | | if (!jsonPropertyInfo.ReadJsonAndSetMember(obj, ref state, ref reader)) |
| | 0 | 231 | | { |
| | 0 | 232 | | state.Current.ReturnValue = obj; |
| | 0 | 233 | | value = default; |
| | 0 | 234 | | return false; |
| | | 235 | | } |
| | 0 | 236 | | } |
| | | 237 | | else |
| | 0 | 238 | | { |
| | 0 | 239 | | if (!jsonPropertyInfo.ReadJsonAndAddExtensionProperty(obj, ref state, ref reader)) |
| | 0 | 240 | | { |
| | | 241 | | // No need to set 'value' here since JsonElement must be read in full. |
| | 0 | 242 | | state.Current.ReturnValue = obj; |
| | 0 | 243 | | value = default; |
| | 0 | 244 | | return false; |
| | | 245 | | } |
| | 0 | 246 | | } |
| | | 247 | | |
| | 0 | 248 | | state.Current.EndProperty(); |
| | 0 | 249 | | } |
| | 0 | 250 | | } |
| | 0 | 251 | | } |
| | | 252 | | |
| | 0 | 253 | | jsonTypeInfo.OnDeserialized?.Invoke(obj); |
| | 0 | 254 | | state.Current.ValidateAllRequiredPropertiesAreRead(jsonTypeInfo); |
| | | 255 | | |
| | | 256 | | // Unbox |
| | 0 | 257 | | Debug.Assert(obj != null); |
| | 0 | 258 | | value = (T)obj; |
| | | 259 | | |
| | | 260 | | // Check if we are trying to update the UTF-8 property cache. |
| | 0 | 261 | | if (state.Current.PropertyRefCacheBuilder != null) |
| | 0 | 262 | | { |
| | 0 | 263 | | jsonTypeInfo.UpdateUtf8PropertyCache(ref state.Current); |
| | 0 | 264 | | } |
| | | 265 | | |
| | 0 | 266 | | return true; |
| | 0 | 267 | | } |
| | | 268 | | |
| | | 269 | | // This method is using aggressive inlining to avoid extra stack frame for deep object graphs. |
| | | 270 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 271 | | internal static void PopulatePropertiesFastPath(object obj, JsonTypeInfo jsonTypeInfo, JsonSerializerOptions opt |
| | 0 | 272 | | { |
| | 0 | 273 | | jsonTypeInfo.OnDeserializing?.Invoke(obj); |
| | 0 | 274 | | state.Current.InitializePropertiesValidationState(jsonTypeInfo); |
| | | 275 | | |
| | | 276 | | // Process all properties. |
| | 0 | 277 | | while (true) |
| | 0 | 278 | | { |
| | | 279 | | // Read the property name or EndObject. |
| | 0 | 280 | | reader.ReadWithVerify(); |
| | | 281 | | |
| | 0 | 282 | | JsonTokenType tokenType = reader.TokenType; |
| | | 283 | | |
| | 0 | 284 | | if (tokenType == JsonTokenType.EndObject) |
| | 0 | 285 | | { |
| | 0 | 286 | | break; |
| | | 287 | | } |
| | | 288 | | |
| | | 289 | | // Read method would have thrown if otherwise. |
| | 0 | 290 | | Debug.Assert(tokenType == JsonTokenType.PropertyName); |
| | | 291 | | |
| | 0 | 292 | | ReadOnlySpan<byte> unescapedPropertyName = JsonSerializer.GetPropertyName(ref state, ref reader, options |
| | 0 | 293 | | Debug.Assert(!isAlreadyReadMetadataProperty, "Only possible for types that can read metadata, which do n |
| | | 294 | | |
| | 0 | 295 | | jsonTypeInfo.ValidateCanBeUsedForPropertyMetadataSerialization(); |
| | 0 | 296 | | JsonPropertyInfo jsonPropertyInfo = JsonSerializer.LookupProperty( |
| | 0 | 297 | | obj, |
| | 0 | 298 | | unescapedPropertyName, |
| | 0 | 299 | | ref state, |
| | 0 | 300 | | options, |
| | 0 | 301 | | out bool useExtensionProperty); |
| | | 302 | | |
| | 0 | 303 | | ReadPropertyValue(obj, ref state, ref reader, jsonPropertyInfo, useExtensionProperty); |
| | 0 | 304 | | } |
| | | 305 | | |
| | 0 | 306 | | jsonTypeInfo.OnDeserialized?.Invoke(obj); |
| | 0 | 307 | | state.Current.ValidateAllRequiredPropertiesAreRead(jsonTypeInfo); |
| | | 308 | | |
| | | 309 | | // Check if we are trying to update the UTF-8 property cache. |
| | 0 | 310 | | if (state.Current.PropertyRefCacheBuilder != null) |
| | 0 | 311 | | { |
| | 0 | 312 | | jsonTypeInfo.UpdateUtf8PropertyCache(ref state.Current); |
| | 0 | 313 | | } |
| | 0 | 314 | | } |
| | | 315 | | |
| | | 316 | | internal sealed override bool OnTryWrite( |
| | | 317 | | Utf8JsonWriter writer, |
| | | 318 | | T value, |
| | | 319 | | JsonSerializerOptions options, |
| | | 320 | | ref WriteStack state) |
| | 0 | 321 | | { |
| | 0 | 322 | | JsonTypeInfo jsonTypeInfo = state.Current.JsonTypeInfo; |
| | 0 | 323 | | jsonTypeInfo.ValidateCanBeUsedForPropertyMetadataSerialization(); |
| | | 324 | | |
| | 0 | 325 | | object obj = value; // box once |
| | | 326 | | |
| | 0 | 327 | | if (!state.SupportContinuation) |
| | 0 | 328 | | { |
| | 0 | 329 | | jsonTypeInfo.OnSerializing?.Invoke(obj); |
| | | 330 | | |
| | 0 | 331 | | writer.WriteStartObject(); |
| | | 332 | | |
| | 0 | 333 | | if (state.CurrentContainsMetadata && CanHaveMetadata) |
| | 0 | 334 | | { |
| | 0 | 335 | | JsonSerializer.WriteMetadataForObject(this, ref state, writer); |
| | 0 | 336 | | } |
| | | 337 | | |
| | 0 | 338 | | foreach (JsonPropertyInfo jsonPropertyInfo in jsonTypeInfo.PropertyCache) |
| | 0 | 339 | | { |
| | 0 | 340 | | if (jsonPropertyInfo.CanSerialize) |
| | 0 | 341 | | { |
| | | 342 | | // Remember the current property for JsonPath support if an exception is thrown. |
| | 0 | 343 | | state.Current.JsonPropertyInfo = jsonPropertyInfo; |
| | 0 | 344 | | state.Current.NumberHandling = jsonPropertyInfo.EffectiveNumberHandling; |
| | | 345 | | |
| | 0 | 346 | | bool success = jsonPropertyInfo.GetMemberAndWriteJson(obj, ref state, writer); |
| | | 347 | | // Converters only return 'false' when out of data which is not possible in fast path. |
| | 0 | 348 | | Debug.Assert(success); |
| | | 349 | | |
| | 0 | 350 | | state.Current.EndProperty(); |
| | 0 | 351 | | } |
| | 0 | 352 | | } |
| | | 353 | | |
| | | 354 | | // Write extension data after the normal properties. |
| | 0 | 355 | | JsonPropertyInfo? extensionDataProperty = jsonTypeInfo.ExtensionDataProperty; |
| | 0 | 356 | | if (extensionDataProperty?.CanSerialize == true) |
| | 0 | 357 | | { |
| | | 358 | | // Remember the current property for JsonPath support if an exception is thrown. |
| | 0 | 359 | | state.Current.JsonPropertyInfo = extensionDataProperty; |
| | 0 | 360 | | state.Current.NumberHandling = extensionDataProperty.EffectiveNumberHandling; |
| | | 361 | | |
| | 0 | 362 | | bool success = extensionDataProperty.GetMemberAndWriteJsonExtensionData(obj, ref state, writer); |
| | 0 | 363 | | Debug.Assert(success); |
| | | 364 | | |
| | 0 | 365 | | state.Current.EndProperty(); |
| | 0 | 366 | | } |
| | | 367 | | |
| | 0 | 368 | | writer.WriteEndObject(); |
| | 0 | 369 | | } |
| | | 370 | | else |
| | 0 | 371 | | { |
| | 0 | 372 | | if (!state.Current.ProcessedStartToken) |
| | 0 | 373 | | { |
| | 0 | 374 | | writer.WriteStartObject(); |
| | | 375 | | |
| | 0 | 376 | | if (state.CurrentContainsMetadata && CanHaveMetadata) |
| | 0 | 377 | | { |
| | 0 | 378 | | JsonSerializer.WriteMetadataForObject(this, ref state, writer); |
| | 0 | 379 | | } |
| | | 380 | | |
| | 0 | 381 | | jsonTypeInfo.OnSerializing?.Invoke(obj); |
| | | 382 | | |
| | 0 | 383 | | state.Current.ProcessedStartToken = true; |
| | 0 | 384 | | } |
| | | 385 | | |
| | 0 | 386 | | ReadOnlySpan<JsonPropertyInfo> propertyCache = jsonTypeInfo.PropertyCache; |
| | 0 | 387 | | while (state.Current.EnumeratorIndex < propertyCache.Length) |
| | 0 | 388 | | { |
| | 0 | 389 | | JsonPropertyInfo jsonPropertyInfo = propertyCache[state.Current.EnumeratorIndex]; |
| | 0 | 390 | | if (jsonPropertyInfo.CanSerialize) |
| | 0 | 391 | | { |
| | 0 | 392 | | state.Current.JsonPropertyInfo = jsonPropertyInfo; |
| | 0 | 393 | | state.Current.NumberHandling = jsonPropertyInfo.EffectiveNumberHandling; |
| | | 394 | | |
| | 0 | 395 | | if (!jsonPropertyInfo.GetMemberAndWriteJson(obj!, ref state, writer)) |
| | 0 | 396 | | { |
| | 0 | 397 | | Debug.Assert(jsonPropertyInfo.EffectiveConverter.ConverterStrategy != ConverterStrategy.Valu |
| | 0 | 398 | | return false; |
| | | 399 | | } |
| | | 400 | | |
| | 0 | 401 | | state.Current.EndProperty(); |
| | 0 | 402 | | state.Current.EnumeratorIndex++; |
| | | 403 | | |
| | 0 | 404 | | if (ShouldFlush(ref state, writer)) |
| | 0 | 405 | | { |
| | 0 | 406 | | return false; |
| | | 407 | | } |
| | 0 | 408 | | } |
| | | 409 | | else |
| | 0 | 410 | | { |
| | 0 | 411 | | state.Current.EnumeratorIndex++; |
| | 0 | 412 | | } |
| | 0 | 413 | | } |
| | | 414 | | |
| | | 415 | | // Write extension data after the normal properties. |
| | 0 | 416 | | if (state.Current.EnumeratorIndex == propertyCache.Length) |
| | 0 | 417 | | { |
| | 0 | 418 | | JsonPropertyInfo? extensionDataProperty = jsonTypeInfo.ExtensionDataProperty; |
| | 0 | 419 | | if (extensionDataProperty?.CanSerialize == true) |
| | 0 | 420 | | { |
| | | 421 | | // Remember the current property for JsonPath support if an exception is thrown. |
| | 0 | 422 | | state.Current.JsonPropertyInfo = extensionDataProperty; |
| | 0 | 423 | | state.Current.NumberHandling = extensionDataProperty.EffectiveNumberHandling; |
| | | 424 | | |
| | 0 | 425 | | if (!extensionDataProperty.GetMemberAndWriteJsonExtensionData(obj, ref state, writer)) |
| | 0 | 426 | | { |
| | 0 | 427 | | return false; |
| | | 428 | | } |
| | | 429 | | |
| | 0 | 430 | | state.Current.EndProperty(); |
| | 0 | 431 | | state.Current.EnumeratorIndex++; |
| | | 432 | | |
| | 0 | 433 | | if (ShouldFlush(ref state, writer)) |
| | 0 | 434 | | { |
| | 0 | 435 | | return false; |
| | | 436 | | } |
| | 0 | 437 | | } |
| | | 438 | | else |
| | 0 | 439 | | { |
| | 0 | 440 | | state.Current.EnumeratorIndex++; |
| | 0 | 441 | | } |
| | 0 | 442 | | } |
| | | 443 | | |
| | 0 | 444 | | if (!state.Current.ProcessedEndToken) |
| | 0 | 445 | | { |
| | 0 | 446 | | state.Current.ProcessedEndToken = true; |
| | 0 | 447 | | writer.WriteEndObject(); |
| | 0 | 448 | | } |
| | 0 | 449 | | } |
| | | 450 | | |
| | 0 | 451 | | jsonTypeInfo.OnSerialized?.Invoke(obj); |
| | | 452 | | |
| | 0 | 453 | | return true; |
| | 0 | 454 | | } |
| | | 455 | | |
| | | 456 | | // AggressiveInlining since this method is only called from two locations and is on a hot path. |
| | | 457 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 458 | | protected static void ReadPropertyValue( |
| | | 459 | | object obj, |
| | | 460 | | scoped ref ReadStack state, |
| | | 461 | | ref Utf8JsonReader reader, |
| | | 462 | | JsonPropertyInfo jsonPropertyInfo, |
| | | 463 | | bool useExtensionProperty) |
| | 0 | 464 | | { |
| | | 465 | | // Skip the property if not found. |
| | 0 | 466 | | if (!jsonPropertyInfo.CanDeserializeOrPopulate) |
| | 0 | 467 | | { |
| | | 468 | | // The Utf8JsonReader.Skip() method will fail fast if it detects that we're reading |
| | | 469 | | // from a partially read buffer, regardless of whether the next value is available. |
| | | 470 | | // This can result in erroneous failures in cases where a custom converter is calling |
| | | 471 | | // into a built-in converter (cf. https://github.com/dotnet/runtime/issues/74108). |
| | | 472 | | // For this reason we need to call the TrySkip() method instead -- the serializer |
| | | 473 | | // should guarantee sufficient read-ahead has been performed for the current object. |
| | 0 | 474 | | bool success = reader.TrySkip(); |
| | 0 | 475 | | Debug.Assert(success, "Serializer should guarantee sufficient read-ahead has been done."); |
| | 0 | 476 | | } |
| | | 477 | | else |
| | 0 | 478 | | { |
| | | 479 | | // Set the property value. |
| | 0 | 480 | | reader.ReadWithVerify(); |
| | | 481 | | |
| | 0 | 482 | | if (!useExtensionProperty) |
| | 0 | 483 | | { |
| | 0 | 484 | | jsonPropertyInfo.ReadJsonAndSetMember(obj, ref state, ref reader); |
| | 0 | 485 | | } |
| | | 486 | | else |
| | 0 | 487 | | { |
| | 0 | 488 | | jsonPropertyInfo.ReadJsonAndAddExtensionProperty(obj, ref state, ref reader); |
| | 0 | 489 | | } |
| | 0 | 490 | | } |
| | | 491 | | |
| | | 492 | | // Ensure any exception thrown in the next read does not have a property in its JsonPath. |
| | 0 | 493 | | state.Current.EndProperty(); |
| | 0 | 494 | | } |
| | | 495 | | |
| | | 496 | | protected static bool ReadAheadPropertyValue(scoped ref ReadStack state, ref Utf8JsonReader reader, JsonProperty |
| | 0 | 497 | | { |
| | | 498 | | // Extension properties can use the JsonElement converter and thus require read-ahead. |
| | 0 | 499 | | bool requiresReadAhead = jsonPropertyInfo.EffectiveConverter.RequiresReadAhead || state.Current.UseExtension |
| | 0 | 500 | | return reader.TryAdvanceWithOptionalReadAhead(requiresReadAhead); |
| | 0 | 501 | | } |
| | | 502 | | } |
| | | 503 | | } |