/
JsonDerivedTypeAttribute.cs
53 lines (48 loc) · 2.4 KB
/
JsonDerivedTypeAttribute.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace System.Text.Json.Serialization
{
/// <summary>
/// When placed on a type declaration, indicates that the specified subtype should be opted into polymorphic serialization.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, AllowMultiple = true, Inherited = false)]
public class JsonDerivedTypeAttribute : JsonAttribute
{
/// <summary>
/// Initializes a new attribute with specified parameters.
/// </summary>
/// <param name="derivedType">A derived type that should be supported in polymorphic serialization of the declared based type.</param>
public JsonDerivedTypeAttribute(Type derivedType)
{
DerivedType = derivedType;
}
/// <summary>
/// Initializes a new attribute with specified parameters.
/// </summary>
/// <param name="derivedType">A derived type that should be supported in polymorphic serialization of the declared base type.</param>
/// <param name="typeDiscriminator">The type discriminator identifier to be used for the serialization of the subtype.</param>
public JsonDerivedTypeAttribute(Type derivedType, string typeDiscriminator)
{
DerivedType = derivedType;
TypeDiscriminator = typeDiscriminator;
}
/// <summary>
/// Initializes a new attribute with specified parameters.
/// </summary>
/// <param name="derivedType">A derived type that should be supported in polymorphic serialization of the declared base type.</param>
/// <param name="typeDiscriminator">The type discriminator identifier to be used for the serialization of the subtype.</param>
public JsonDerivedTypeAttribute(Type derivedType, int typeDiscriminator)
{
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; }
}
}