| | | 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.CodeAnalysis; |
| | | 6 | | using System.Reflection; |
| | | 7 | | |
| | | 8 | | namespace System.Text.Json.Serialization.Metadata |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Defines polymorphic configuration for a specified base type. |
| | | 12 | | /// </summary> |
| | | 13 | | public class JsonPolymorphismOptions |
| | | 14 | | { |
| | | 15 | | private DerivedTypeList? _derivedTypes; |
| | | 16 | | private bool _isConfigured; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Creates an empty <see cref="JsonPolymorphismOptions"/> instance. |
| | | 20 | | /// </summary> |
| | 0 | 21 | | public JsonPolymorphismOptions() |
| | 0 | 22 | | { |
| | 0 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Gets the list of derived types supported in the current polymorphic type configuration. |
| | | 27 | | /// </summary> |
| | 0 | 28 | | public IList<JsonDerivedType> DerivedTypes => _derivedTypes ??= new(this); |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// When set to <see langword="true"/>, instructs the serializer to ignore any |
| | | 32 | | /// unrecognized type discriminator id's and reverts to the contract of the base type. |
| | | 33 | | /// Otherwise, it will fail the deserialization. |
| | | 34 | | /// </summary> |
| | | 35 | | /// <exception cref="InvalidOperationException"> |
| | | 36 | | /// The parent <see cref="JsonTypeInfo"/> instance has been locked for further modification. |
| | | 37 | | /// </exception> |
| | | 38 | | public bool IgnoreUnrecognizedTypeDiscriminators |
| | | 39 | | { |
| | 0 | 40 | | get; |
| | | 41 | | set |
| | 0 | 42 | | { |
| | 0 | 43 | | VerifyMutable(); |
| | 0 | 44 | | _isConfigured = true; |
| | 0 | 45 | | field = value; |
| | 0 | 46 | | } |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Gets or sets the behavior when serializing an undeclared derived runtime type. |
| | | 51 | | /// </summary> |
| | | 52 | | /// <exception cref="InvalidOperationException"> |
| | | 53 | | /// The parent <see cref="JsonTypeInfo"/> instance has been locked for further modification. |
| | | 54 | | /// </exception> |
| | | 55 | | public JsonUnknownDerivedTypeHandling UnknownDerivedTypeHandling |
| | | 56 | | { |
| | 0 | 57 | | get; |
| | | 58 | | set |
| | 0 | 59 | | { |
| | 0 | 60 | | VerifyMutable(); |
| | 0 | 61 | | _isConfigured = true; |
| | 0 | 62 | | field = value; |
| | 0 | 63 | | } |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Gets or sets a custom type discriminator property name for the polymorhic type. |
| | | 68 | | /// Uses the default '$type' property name if left unset. |
| | | 69 | | /// </summary> |
| | | 70 | | /// <exception cref="InvalidOperationException"> |
| | | 71 | | /// The parent <see cref="JsonTypeInfo"/> instance has been locked for further modification. |
| | | 72 | | /// </exception> |
| | | 73 | | [AllowNull] |
| | | 74 | | public string TypeDiscriminatorPropertyName |
| | | 75 | | { |
| | 0 | 76 | | get => field ?? JsonSerializer.TypePropertyName; |
| | | 77 | | set |
| | 0 | 78 | | { |
| | 0 | 79 | | VerifyMutable(); |
| | 0 | 80 | | _isConfigured = true; |
| | 0 | 81 | | field = value; |
| | 0 | 82 | | } |
| | | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | private void VerifyMutable() => DeclaringTypeInfo?.VerifyMutable(); |
| | | 86 | | |
| | 0 | 87 | | internal JsonTypeInfo? DeclaringTypeInfo { get; set; } |
| | | 88 | | |
| | 0 | 89 | | internal bool IsEmpty => !_isConfigured && _derivedTypes is not { Count: > 0 }; |
| | | 90 | | |
| | | 91 | | internal static JsonPolymorphismOptions? CreateFromAttributeDeclarations( |
| | | 92 | | Type baseType, |
| | | 93 | | out JsonPolymorphicAttribute? polymorphicAttribute) |
| | 0 | 94 | | { |
| | 0 | 95 | | JsonPolymorphismOptions? options = null; |
| | 0 | 96 | | polymorphicAttribute = baseType.GetCustomAttribute<JsonPolymorphicAttribute>(inherit: false); |
| | | 97 | | |
| | 0 | 98 | | if (polymorphicAttribute is not null) |
| | 0 | 99 | | { |
| | 0 | 100 | | options = new() |
| | 0 | 101 | | { |
| | 0 | 102 | | IgnoreUnrecognizedTypeDiscriminators = polymorphicAttribute.IgnoreUnrecognizedTypeDiscriminators, |
| | 0 | 103 | | UnknownDerivedTypeHandling = polymorphicAttribute.UnknownDerivedTypeHandling, |
| | 0 | 104 | | TypeDiscriminatorPropertyName = polymorphicAttribute.TypeDiscriminatorPropertyName, |
| | 0 | 105 | | }; |
| | 0 | 106 | | } |
| | | 107 | | |
| | 0 | 108 | | foreach (JsonDerivedTypeAttribute attr in baseType.GetCustomAttributes<JsonDerivedTypeAttribute>(inherit: fa |
| | 0 | 109 | | { |
| | 0 | 110 | | (options ??= new()).DerivedTypes.Add(new JsonDerivedType(attr.DerivedType, attr.TypeDiscriminator)); |
| | 0 | 111 | | } |
| | | 112 | | |
| | 0 | 113 | | return options; |
| | 0 | 114 | | } |
| | | 115 | | |
| | | 116 | | private sealed class DerivedTypeList : ConfigurationList<JsonDerivedType> |
| | | 117 | | { |
| | | 118 | | private readonly JsonPolymorphismOptions _parent; |
| | | 119 | | |
| | 0 | 120 | | public DerivedTypeList(JsonPolymorphismOptions parent) |
| | 0 | 121 | | { |
| | 0 | 122 | | _parent = parent; |
| | 0 | 123 | | } |
| | | 124 | | |
| | 0 | 125 | | public override bool IsReadOnly => _parent.DeclaringTypeInfo?.IsReadOnly == true; |
| | 0 | 126 | | protected override void OnCollectionModifying() => _parent.DeclaringTypeInfo?.VerifyMutable(); |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | } |
| | | 130 | | } |
| | | 131 | | |