| | | 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.Text.Json.Serialization.Metadata; |
| | | 5 | | |
| | | 6 | | namespace System.Text.Json.Serialization |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// When implemented in a derived class, creates a <see cref="JsonTypeClassifier"/> |
| | | 10 | | /// delegate that classifies JSON payloads to determine the target type. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// <para> |
| | | 14 | | /// This factory is the extension point for users who want to customize how JSON payloads |
| | | 15 | | /// are matched to union case types or polymorphic derived types during deserialization. |
| | | 16 | | /// It is referenced by the <see cref="JsonUnionAttribute.TypeClassifier"/> and |
| | | 17 | | /// <see cref="JsonPolymorphicAttribute.TypeClassifier"/> properties. |
| | | 18 | | /// </para> |
| | | 19 | | /// <para> |
| | | 20 | | /// The factory's <see cref="CreateJsonClassifier"/> method is called once during |
| | | 21 | | /// <see cref="JsonTypeInfo"/> configuration, and the returned delegate is invoked |
| | | 22 | | /// on every deserialization call. |
| | | 23 | | /// </para> |
| | | 24 | | /// <para> |
| | | 25 | | /// A single factory may produce type classifiers for multiple declaring types — analogous to |
| | | 26 | | /// <see cref="JsonConverterFactory"/>. Implementations report supported metadata shapes via |
| | | 27 | | /// <see cref="CanClassify"/>. For the common case of a factory pinned to a single declaring |
| | | 28 | | /// type, derive from <see cref="JsonTypeClassifierFactory{T}"/> instead. |
| | | 29 | | /// </para> |
| | | 30 | | /// <para> |
| | | 31 | | /// Factories may be registered globally on <see cref="JsonSerializerOptions.TypeClassifiers"/> |
| | | 32 | | /// or per-type via <see cref="JsonUnionAttribute.TypeClassifier"/> / |
| | | 33 | | /// <see cref="JsonPolymorphicAttribute.TypeClassifier"/>. When both are present the attribute wins. |
| | | 34 | | /// </para> |
| | | 35 | | /// <para> |
| | | 36 | | /// For union types, the produced classifier delegate is <b>not</b> invoked when the |
| | | 37 | | /// payload is a JSON <see cref="JsonTokenType.Null"/> token. The converter handles |
| | | 38 | | /// null payloads directly: it produces the canonical null union value when at least |
| | | 39 | | /// one case is nullable, and otherwise throws a <see cref="JsonException"/>. See |
| | | 40 | | /// <see cref="JsonTypeClassifier"/> for details. |
| | | 41 | | /// </para> |
| | | 42 | | /// </remarks> |
| | | 43 | | public abstract class JsonTypeClassifierFactory |
| | | 44 | | { |
| | | 45 | | /// <summary> |
| | | 46 | | /// When overridden, indicates whether this factory can produce a classifier for |
| | | 47 | | /// the specified metadata context. |
| | | 48 | | /// </summary> |
| | | 49 | | /// <param name="context">The metadata context for the type being configured.</param> |
| | | 50 | | /// <returns> |
| | | 51 | | /// <see langword="true"/> if this factory can produce a classifier for |
| | | 52 | | /// <paramref name="context"/>; otherwise <see langword="false"/>. |
| | | 53 | | /// </returns> |
| | | 54 | | public abstract bool CanClassify(JsonTypeClassifierContext context); |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Creates a delegate that classifies JSON payloads to determine the target type. |
| | | 58 | | /// </summary> |
| | | 59 | | /// <param name="context"> |
| | | 60 | | /// An immutable snapshot of metadata including the declaring type, candidate types |
| | | 61 | | /// (with optional discriminator values), and the discriminator property name. |
| | | 62 | | /// </param> |
| | | 63 | | /// <param name="options">The serializer options for the current context.</param> |
| | | 64 | | /// <returns>A <see cref="JsonTypeClassifier"/> delegate.</returns> |
| | | 65 | | public abstract JsonTypeClassifier CreateJsonClassifier( |
| | | 66 | | JsonTypeClassifierContext context, |
| | | 67 | | JsonSerializerOptions options); |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Convenience base class for a <see cref="JsonTypeClassifierFactory"/> that produces |
| | | 72 | | /// type classifiers for a single declaring type <typeparamref name="T"/>. |
| | | 73 | | /// </summary> |
| | | 74 | | /// <typeparam name="T">The declaring type that this factory classifies.</typeparam> |
| | | 75 | | /// <remarks> |
| | | 76 | | /// Seals <see cref="CanClassify"/> to compare the context declaring type against |
| | | 77 | | /// <typeparamref name="T"/> using reference equality. Subclasses only need to implement |
| | | 78 | | /// <see cref="JsonTypeClassifierFactory.CreateJsonClassifier"/>. |
| | | 79 | | /// </remarks> |
| | | 80 | | public abstract class JsonTypeClassifierFactory<T> : JsonTypeClassifierFactory |
| | | 81 | | { |
| | | 82 | | /// <inheritdoc/> |
| | 0 | 83 | | public sealed override bool CanClassify(JsonTypeClassifierContext context) => context.DeclaringType == typeof(T) |
| | | 84 | | } |
| | | 85 | | } |
| | | 86 | | |