| | | 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.Text.Json.Serialization.Metadata; |
| | | 8 | | |
| | | 9 | | namespace System.Text.Json.Serialization.Converters |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Implementation of <cref>JsonObjectConverter{T}</cref> that supports the deserialization |
| | | 13 | | /// of JSON objects using parameterized constructors. |
| | | 14 | | /// </summary> |
| | | 15 | | internal class LargeObjectWithParameterizedConstructorConverter<T> : ObjectWithParameterizedConstructorConverter<T> |
| | | 16 | | { |
| | | 17 | | protected sealed override bool ReadAndCacheConstructorArgument(scoped ref ReadStack state, ref Utf8JsonReader re |
| | 0 | 18 | | { |
| | 0 | 19 | | Debug.Assert(jsonParameterInfo.ShouldDeserialize); |
| | | 20 | | |
| | 0 | 21 | | bool success = jsonParameterInfo.EffectiveConverter.TryReadAsObject(ref reader, jsonParameterInfo.ParameterT |
| | | 22 | | |
| | 0 | 23 | | if (success && !(arg == null && jsonParameterInfo.IgnoreNullTokensOnRead)) |
| | 0 | 24 | | { |
| | 0 | 25 | | if (arg == null && !jsonParameterInfo.IsNullable && jsonParameterInfo.Options.RespectNullableAnnotations |
| | 0 | 26 | | { |
| | 0 | 27 | | ThrowHelper.ThrowJsonException_ConstructorParameterDisallowNull(jsonParameterInfo.Name, state.Curren |
| | | 28 | | } |
| | | 29 | | |
| | 0 | 30 | | ((object[])state.Current.CtorArgumentState!.Arguments)[jsonParameterInfo.Position] = arg!; |
| | 0 | 31 | | } |
| | | 32 | | |
| | 0 | 33 | | return success; |
| | 0 | 34 | | } |
| | | 35 | | |
| | | 36 | | protected sealed override object CreateObject(ref ReadStackFrame frame) |
| | 0 | 37 | | { |
| | 0 | 38 | | Debug.Assert(frame.CtorArgumentState != null); |
| | 0 | 39 | | Debug.Assert(frame.JsonTypeInfo.CreateObjectWithArgs != null); |
| | | 40 | | |
| | 0 | 41 | | object[] arguments = (object[])frame.CtorArgumentState.Arguments; |
| | 0 | 42 | | frame.CtorArgumentState.Arguments = null!; |
| | | 43 | | |
| | 0 | 44 | | Func<object[], T> createObject = (Func<object[], T>)frame.JsonTypeInfo.CreateObjectWithArgs; |
| | | 45 | | |
| | 0 | 46 | | object obj = createObject(arguments); |
| | | 47 | | |
| | 0 | 48 | | ArrayPool<object>.Shared.Return(arguments, clearArray: true); |
| | 0 | 49 | | return obj; |
| | 0 | 50 | | } |
| | | 51 | | |
| | | 52 | | protected sealed override void InitializeConstructorArgumentCaches(ref ReadStack state, JsonSerializerOptions op |
| | 8 | 53 | | { |
| | 8 | 54 | | JsonTypeInfo typeInfo = state.Current.JsonTypeInfo; |
| | | 55 | | |
| | 8 | 56 | | object?[] arguments = ArrayPool<object>.Shared.Rent(typeInfo.ParameterCache.Length); |
| | 120 | 57 | | foreach (JsonParameterInfo parameterInfo in typeInfo.ParameterCache) |
| | 48 | 58 | | { |
| | 48 | 59 | | arguments[parameterInfo.Position] = parameterInfo.EffectiveDefaultValue; |
| | 48 | 60 | | } |
| | | 61 | | |
| | 8 | 62 | | state.Current.CtorArgumentState!.Arguments = arguments; |
| | 8 | 63 | | } |
| | | 64 | | } |
| | | 65 | | } |