| | | 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 _ignoreUnrecognizedTypeDiscriminators; |
| | | 17 | | private JsonUnknownDerivedTypeHandling _unknownDerivedTypeHandling; |
| | | 18 | | private string? _typeDiscriminatorPropertyName; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Creates an empty <see cref="JsonPolymorphismOptions"/> instance. |
| | | 22 | | /// </summary> |
| | 0 | 23 | | public JsonPolymorphismOptions() |
| | 0 | 24 | | { |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Gets the list of derived types supported in the current polymorphic type configuration. |
| | | 29 | | /// </summary> |
| | 0 | 30 | | public IList<JsonDerivedType> DerivedTypes => _derivedTypes ??= new(this); |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// When set to <see langword="true"/>, instructs the serializer to ignore any |
| | | 34 | | /// unrecognized type discriminator id's and reverts to the contract of the base type. |
| | | 35 | | /// Otherwise, it will fail the deserialization. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <exception cref="InvalidOperationException"> |
| | | 38 | | /// The parent <see cref="JsonTypeInfo"/> instance has been locked for further modification. |
| | | 39 | | /// </exception> |
| | | 40 | | public bool IgnoreUnrecognizedTypeDiscriminators |
| | | 41 | | { |
| | 0 | 42 | | get => _ignoreUnrecognizedTypeDiscriminators; |
| | | 43 | | set |
| | 0 | 44 | | { |
| | 0 | 45 | | VerifyMutable(); |
| | 0 | 46 | | _ignoreUnrecognizedTypeDiscriminators = value; |
| | 0 | 47 | | } |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Gets or sets the behavior when serializing an undeclared derived runtime type. |
| | | 52 | | /// </summary> |
| | | 53 | | /// <exception cref="InvalidOperationException"> |
| | | 54 | | /// The parent <see cref="JsonTypeInfo"/> instance has been locked for further modification. |
| | | 55 | | /// </exception> |
| | | 56 | | public JsonUnknownDerivedTypeHandling UnknownDerivedTypeHandling |
| | | 57 | | { |
| | 0 | 58 | | get => _unknownDerivedTypeHandling; |
| | | 59 | | set |
| | 0 | 60 | | { |
| | 0 | 61 | | VerifyMutable(); |
| | 0 | 62 | | _unknownDerivedTypeHandling = 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 => _typeDiscriminatorPropertyName ?? JsonSerializer.TypePropertyName; |
| | | 77 | | set |
| | 0 | 78 | | { |
| | 0 | 79 | | VerifyMutable(); |
| | 0 | 80 | | _typeDiscriminatorPropertyName = value; |
| | 0 | 81 | | } |
| | | 82 | | } |
| | | 83 | | |
| | 0 | 84 | | private void VerifyMutable() => DeclaringTypeInfo?.VerifyMutable(); |
| | | 85 | | |
| | 0 | 86 | | internal JsonTypeInfo? DeclaringTypeInfo { get; set; } |
| | | 87 | | |
| | | 88 | | private sealed class DerivedTypeList : ConfigurationList<JsonDerivedType> |
| | | 89 | | { |
| | | 90 | | private readonly JsonPolymorphismOptions _parent; |
| | | 91 | | |
| | 0 | 92 | | public DerivedTypeList(JsonPolymorphismOptions parent) |
| | 0 | 93 | | { |
| | 0 | 94 | | _parent = parent; |
| | 0 | 95 | | } |
| | | 96 | | |
| | 0 | 97 | | public override bool IsReadOnly => _parent.DeclaringTypeInfo?.IsReadOnly == true; |
| | 0 | 98 | | protected override void OnCollectionModifying() => _parent.DeclaringTypeInfo?.VerifyMutable(); |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | internal static JsonPolymorphismOptions? CreateFromAttributeDeclarations(Type baseType) |
| | 12254 | 102 | | { |
| | 12254 | 103 | | JsonPolymorphismOptions? options = null; |
| | | 104 | | |
| | 12254 | 105 | | if (baseType.GetCustomAttribute<JsonPolymorphicAttribute>(inherit: false) is JsonPolymorphicAttribute polymo |
| | 0 | 106 | | { |
| | 0 | 107 | | options = new() |
| | 0 | 108 | | { |
| | 0 | 109 | | IgnoreUnrecognizedTypeDiscriminators = polymorphicAttribute.IgnoreUnrecognizedTypeDiscriminators, |
| | 0 | 110 | | UnknownDerivedTypeHandling = polymorphicAttribute.UnknownDerivedTypeHandling, |
| | 0 | 111 | | TypeDiscriminatorPropertyName = polymorphicAttribute.TypeDiscriminatorPropertyName, |
| | 0 | 112 | | }; |
| | 0 | 113 | | } |
| | | 114 | | |
| | 36762 | 115 | | foreach (JsonDerivedTypeAttribute attr in baseType.GetCustomAttributes<JsonDerivedTypeAttribute>(inherit: fa |
| | 0 | 116 | | { |
| | 0 | 117 | | (options ??= new()).DerivedTypes.Add(new JsonDerivedType(attr.DerivedType, attr.TypeDiscriminator)); |
| | 0 | 118 | | } |
| | | 119 | | |
| | 12254 | 120 | | return options; |
| | 12254 | 121 | | } |
| | | 122 | | } |
| | | 123 | | } |