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