| | | 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.Collections.Generic; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Diagnostics.CodeAnalysis; |
| | | 8 | | using System.Reflection; |
| | | 9 | | using System.Text.Json.Reflection; |
| | | 10 | | using System.Text.Json.Serialization.Metadata; |
| | | 11 | | |
| | | 12 | | namespace System.Text.Json.Serialization.Converters |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Converter factory for all object-based types (non-enumerable and non-primitive). |
| | | 16 | | /// </summary> |
| | | 17 | | [RequiresDynamicCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)] |
| | | 18 | | internal sealed class ObjectConverterFactory : JsonConverterFactory |
| | | 19 | | { |
| | | 20 | | // Need to toggle this behavior when generating converters for F# struct records. |
| | | 21 | | private readonly bool _useDefaultConstructorInUnannotatedStructs; |
| | | 22 | | |
| | | 23 | | [RequiresUnreferencedCode(JsonSerializer.SerializationUnreferencedCodeMessage)] |
| | 1 | 24 | | public ObjectConverterFactory(bool useDefaultConstructorInUnannotatedStructs = true) |
| | 1 | 25 | | { |
| | 1 | 26 | | _useDefaultConstructorInUnannotatedStructs = useDefaultConstructorInUnannotatedStructs; |
| | 1 | 27 | | } |
| | | 28 | | |
| | | 29 | | public override bool CanConvert(Type typeToConvert) |
| | 2598 | 30 | | { |
| | | 31 | | // This is the last built-in factory converter, so if the IEnumerableConverterFactory doesn't |
| | | 32 | | // support it, then it is not IEnumerable. |
| | 2598 | 33 | | Debug.Assert(!typeof(IEnumerable).IsAssignableFrom(typeToConvert)); |
| | 2598 | 34 | | return true; |
| | 2598 | 35 | | } |
| | | 36 | | |
| | | 37 | | [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", |
| | | 38 | | Justification = "The ctor is marked RequiresUnreferencedCode.")] |
| | | 39 | | [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2067:UnrecognizedReflectionPattern", |
| | | 40 | | Justification = "The ctor is marked RequiresUnreferencedCode.")] |
| | | 41 | | public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) |
| | 1299 | 42 | | { |
| | | 43 | | JsonConverter converter; |
| | | 44 | | Type converterType; |
| | | 45 | | |
| | 1299 | 46 | | bool useDefaultConstructorInUnannotatedStructs = _useDefaultConstructorInUnannotatedStructs && !typeToConver |
| | 1299 | 47 | | if (!typeToConvert.TryGetDeserializationConstructor(useDefaultConstructorInUnannotatedStructs, out Construct |
| | 0 | 48 | | { |
| | 0 | 49 | | ThrowHelper.ThrowInvalidOperationException_SerializationDuplicateTypeAttribute<JsonConstructorAttribute> |
| | | 50 | | } |
| | | 51 | | |
| | 1299 | 52 | | ParameterInfo[]? parameters = constructor?.GetParameters(); |
| | | 53 | | |
| | 1299 | 54 | | if (constructor == null || typeToConvert.IsAbstract || parameters!.Length == 0) |
| | 550 | 55 | | { |
| | 550 | 56 | | converterType = typeof(ObjectDefaultConverter<>).MakeGenericType(typeToConvert); |
| | 550 | 57 | | } |
| | | 58 | | else |
| | 749 | 59 | | { |
| | 749 | 60 | | int parameterCount = parameters.Length; |
| | | 61 | | |
| | 11235 | 62 | | foreach (ParameterInfo parameter in parameters) |
| | 4494 | 63 | | { |
| | | 64 | | // Every argument must be of supported type. |
| | 4494 | 65 | | JsonTypeInfo.ValidateType(parameter.ParameterType); |
| | 4494 | 66 | | } |
| | | 67 | | |
| | 749 | 68 | | if (parameterCount <= JsonConstants.UnboxedParameterCountThreshold) |
| | 0 | 69 | | { |
| | 0 | 70 | | Type placeHolderType = JsonTypeInfo.ObjectType; |
| | 0 | 71 | | Type[] typeArguments = new Type[JsonConstants.UnboxedParameterCountThreshold + 1]; |
| | | 72 | | |
| | 0 | 73 | | typeArguments[0] = typeToConvert; |
| | 0 | 74 | | for (int i = 0; i < JsonConstants.UnboxedParameterCountThreshold; i++) |
| | 0 | 75 | | { |
| | 0 | 76 | | if (i < parameterCount) |
| | 0 | 77 | | { |
| | 0 | 78 | | typeArguments[i + 1] = parameters[i].ParameterType; |
| | 0 | 79 | | } |
| | | 80 | | else |
| | 0 | 81 | | { |
| | | 82 | | // Use placeholder arguments if there are less args than the threshold. |
| | 0 | 83 | | typeArguments[i + 1] = placeHolderType; |
| | 0 | 84 | | } |
| | 0 | 85 | | } |
| | | 86 | | |
| | 0 | 87 | | converterType = typeof(SmallObjectWithParameterizedConstructorConverter<,,,,>).MakeGenericType(typeA |
| | 0 | 88 | | } |
| | | 89 | | else |
| | 749 | 90 | | { |
| | 749 | 91 | | converterType = typeof(LargeObjectWithParameterizedConstructorConverterWithReflection<>).MakeGeneric |
| | 749 | 92 | | } |
| | 749 | 93 | | } |
| | | 94 | | |
| | 1299 | 95 | | converter = (JsonConverter)Activator.CreateInstance( |
| | 1299 | 96 | | converterType, |
| | 1299 | 97 | | BindingFlags.Instance | BindingFlags.Public, |
| | 1299 | 98 | | binder: null, |
| | 1299 | 99 | | args: null, |
| | 1299 | 100 | | culture: null)!; |
| | | 101 | | |
| | 1299 | 102 | | converter.ConstructorInfo = constructor!; |
| | 1299 | 103 | | return converter; |
| | 1299 | 104 | | } |
| | | 105 | | } |
| | | 106 | | } |