| | | 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.Text.Json.Serialization.Metadata; |
| | | 6 | | |
| | | 7 | | namespace System.Text.Json.Serialization |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Provides immutable metadata to a <see cref="JsonTypeClassifierFactory"/> when |
| | | 11 | | /// creating a <see cref="JsonTypeClassifier"/> delegate. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks> |
| | | 14 | | /// <para> |
| | | 15 | | /// The context carries a <see cref="Kind"/> value indicating whether the classifier |
| | | 16 | | /// is being created for a union type or a polymorphic type. Exactly one of the two |
| | | 17 | | /// candidate lists is populated for any given context. |
| | | 18 | | /// </para> |
| | | 19 | | /// <para> |
| | | 20 | | /// Instances are created internally by the serialization infrastructure. Users |
| | | 21 | | /// interact with the context through a <see cref="JsonTypeClassifierFactory"/> |
| | | 22 | | /// implementation. |
| | | 23 | | /// </para> |
| | | 24 | | /// </remarks> |
| | | 25 | | public sealed class JsonTypeClassifierContext |
| | | 26 | | { |
| | | 27 | | /// <summary> |
| | | 28 | | /// Initializes a new instance of the <see cref="JsonTypeClassifierContext"/> class. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="kind">The type of classifier metadata being configured.</param> |
| | | 31 | | /// <param name="declaringTypeInfo">The contract being configured for classification.</param> |
| | | 32 | | /// <param name="unionCases">The union cases of the declaring type, or an empty list.</param> |
| | | 33 | | /// <param name="derivedTypes">The derived types of the declaring type, or an empty list.</param> |
| | | 34 | | /// <param name="typeDiscriminatorPropertyName">The JSON property name used for type discrimination, or <see lan |
| | 0 | 35 | | internal JsonTypeClassifierContext( |
| | 0 | 36 | | JsonTypeClassifierKind kind, |
| | 0 | 37 | | JsonTypeInfo declaringTypeInfo, |
| | 0 | 38 | | IReadOnlyList<JsonUnionCaseInfo> unionCases, |
| | 0 | 39 | | IReadOnlyList<JsonDerivedType> derivedTypes, |
| | 0 | 40 | | string? typeDiscriminatorPropertyName) |
| | 0 | 41 | | { |
| | 0 | 42 | | Kind = kind; |
| | 0 | 43 | | DeclaringTypeInfo = declaringTypeInfo; |
| | 0 | 44 | | UnionCases = unionCases; |
| | 0 | 45 | | DerivedTypes = derivedTypes; |
| | 0 | 46 | | TypeDiscriminatorPropertyName = typeDiscriminatorPropertyName; |
| | 0 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Gets the type of classifier metadata being configured. |
| | | 51 | | /// </summary> |
| | 0 | 52 | | public JsonTypeClassifierKind Kind { get; } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Gets the type being configured for classification. |
| | | 56 | | /// </summary> |
| | | 57 | | /// <remarks> |
| | | 58 | | /// For polymorphic types, this is the base class (e.g., <c>Animal</c>). |
| | | 59 | | /// For union types, this is the union type (e.g., <c>IntOrString</c>). |
| | | 60 | | /// </remarks> |
| | 0 | 61 | | public Type DeclaringType => DeclaringTypeInfo.Type; |
| | | 62 | | |
| | | 63 | | // The contract may still be mutable when a modifier reads TypeClassifier. |
| | 0 | 64 | | internal JsonTypeInfo DeclaringTypeInfo { get; } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Gets the union cases of <see cref="DeclaringType"/>. |
| | | 68 | | /// </summary> |
| | | 69 | | /// <remarks> |
| | | 70 | | /// Non-empty when <see cref="DeclaringType"/> is configured as a union type. The |
| | | 71 | | /// list mirrors <see cref="JsonTypeInfo.UnionCases"/> on the resolved |
| | | 72 | | /// <see cref="JsonTypeInfo"/>. |
| | | 73 | | /// </remarks> |
| | 0 | 74 | | public IReadOnlyList<JsonUnionCaseInfo> UnionCases { get; } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Gets the derived types of <see cref="DeclaringType"/>. |
| | | 78 | | /// </summary> |
| | | 79 | | /// <remarks> |
| | | 80 | | /// Non-empty when <see cref="DeclaringType"/> is configured as a polymorphic |
| | | 81 | | /// type. The list mirrors |
| | | 82 | | /// <see cref="Metadata.JsonPolymorphismOptions.DerivedTypes"/>; each entry may |
| | | 83 | | /// carry a <see cref="JsonDerivedType.TypeDiscriminator"/> string or integer. |
| | | 84 | | /// </remarks> |
| | 0 | 85 | | public IReadOnlyList<JsonDerivedType> DerivedTypes { get; } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Gets the JSON property name used for type discrimination (e.g., <c>"$type"</c>, <c>"kind"</c>). |
| | | 89 | | /// </summary> |
| | | 90 | | /// <remarks> |
| | | 91 | | /// Populated from <see cref="Metadata.JsonPolymorphismOptions.TypeDiscriminatorPropertyName"/> |
| | | 92 | | /// for polymorphic types. <see langword="null"/> for union types (unions don't use |
| | | 93 | | /// discriminator properties by default). |
| | | 94 | | /// </remarks> |
| | 0 | 95 | | public string? TypeDiscriminatorPropertyName { get; } |
| | | 96 | | } |
| | | 97 | | } |
| | | 98 | | |