| | | 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.Diagnostics; |
| | | 5 | | |
| | | 6 | | namespace System.Text.Json.Serialization.Metadata |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// Represents a supported derived type defined in the metadata of a polymorphic type. |
| | | 10 | | /// </summary> |
| | | 11 | | public readonly struct JsonDerivedType |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Specifies a supported derived type without a type discriminator. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <param name="derivedType">The derived type to be supported by the polymorphic type metadata.</param> |
| | | 17 | | public JsonDerivedType(Type derivedType) |
| | 0 | 18 | | { |
| | 0 | 19 | | DerivedType = derivedType; |
| | 0 | 20 | | TypeDiscriminator = null; |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Specifies a supported derived type with an integer type discriminator. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="derivedType">The derived type to be supported by the polymorphic type metadata.</param> |
| | | 27 | | /// <param name="typeDiscriminator">The type discriminator to be associated with the derived type.</param> |
| | | 28 | | public JsonDerivedType(Type derivedType, int typeDiscriminator) |
| | 0 | 29 | | { |
| | 0 | 30 | | DerivedType = derivedType; |
| | 0 | 31 | | TypeDiscriminator = typeDiscriminator; |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Specifies a supported derived type with a string type discriminator. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="derivedType">The derived type to be supported by the polymorphic type metadata.</param> |
| | | 38 | | /// <param name="typeDiscriminator">The type discriminator to be associated with the derived type.</param> |
| | | 39 | | public JsonDerivedType(Type derivedType, string typeDiscriminator) |
| | 0 | 40 | | { |
| | 0 | 41 | | DerivedType = derivedType; |
| | 0 | 42 | | TypeDiscriminator = typeDiscriminator; |
| | 0 | 43 | | } |
| | | 44 | | |
| | | 45 | | internal JsonDerivedType(Type derivedType, object? typeDiscriminator) |
| | 0 | 46 | | { |
| | 0 | 47 | | Debug.Assert(typeDiscriminator is null or int or string); |
| | 0 | 48 | | DerivedType = derivedType; |
| | 0 | 49 | | TypeDiscriminator = typeDiscriminator; |
| | 0 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// A derived type that should be supported in polymorphic serialization of the declared base type. |
| | | 54 | | /// </summary> |
| | 0 | 55 | | public Type DerivedType { get; } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// The type discriminator identifier to be used for the serialization of the subtype. |
| | | 59 | | /// </summary> |
| | 0 | 60 | | public object? TypeDiscriminator { get; } |
| | | 61 | | |
| | | 62 | | internal void Deconstruct(out Type derivedType, out object? typeDiscriminator) |
| | 0 | 63 | | { |
| | 0 | 64 | | derivedType = DerivedType; |
| | 0 | 65 | | typeDiscriminator = TypeDiscriminator; |
| | 0 | 66 | | } |
| | | 67 | | } |
| | | 68 | | } |