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