-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathNotImplementedEmbed.cs
More file actions
96 lines (87 loc) · 3.66 KB
/
Copy pathNotImplementedEmbed.cs
File metadata and controls
96 lines (87 loc) · 3.66 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System.Diagnostics.CodeAnalysis;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
namespace Kook;
/// <summary>
/// 表示一个消息中未能被解析为已知的强类型的嵌入式内容。
/// </summary>
public struct NotImplementedEmbed : IEmbed
{
internal NotImplementedEmbed(string rawType, JsonNode jsonNode)
{
RawType = rawType;
JsonNode = jsonNode;
}
/// <inheritdoc />
public EmbedType Type => EmbedType.NotImplemented;
/// <summary>
/// 获取嵌入式内容的类型的原始值。
/// </summary>
public string RawType { get; }
/// <summary>
/// 获取嵌入式内容的原始 JSON。
/// </summary>
public JsonNode JsonNode { get; }
/// <summary>
/// 通过 JSON 反序列化将嵌入式内容解析为具体类型。
/// </summary>
/// <param name="options"> 用于反序列化操作的选项。 </param>
/// <typeparam name="T"> 要解析为的具体类型。 </typeparam>
/// <returns> 解析后的嵌入式内容。 </returns>
#if NET5_0_OR_GREATER
[RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a resolving function for AOT applications.")]
[RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use the overload that takes a resolving function for AOT applications.")]
#endif
public T? Resolve<T>(JsonSerializerOptions? options = null)
where T : IEmbed
{
options ??= new JsonSerializerOptions
{
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
NumberHandling = JsonNumberHandling.AllowReadingFromString
};
T? embed = JsonNode.Deserialize<T>(options);
return embed;
}
/// <summary>
/// 通过 JSON 反序列化将嵌入式内容解析为具体类型。
/// </summary>
/// <param name="jsonTypeInfo"> 用于反序列化操作的类型信息。 </param>
/// <typeparam name="T"> 要解析为的具体类型。 </typeparam>
/// <returns> 解析后的嵌入式内容。 </returns>
public T? Resolve<T>(JsonTypeInfo jsonTypeInfo)
where T : IEmbed
{
if (jsonTypeInfo is not JsonTypeInfo<T> typedJsonTypeInfo)
throw new ArgumentException($"The provided JsonTypeInfo is not of the expected type {typeof(T).FullName}.", nameof(jsonTypeInfo));
T? embed = JsonNode.Deserialize(typedJsonTypeInfo);
return embed;
}
/// <summary>
/// 通过 JSON 反序列化将嵌入式内容解析为具体类型。
/// </summary>
/// <param name="jsonTypeInfo"> 用于反序列化操作的类型信息。 </param>
/// <typeparam name="T"> 要解析为的具体类型。 </typeparam>
/// <returns> 解析后的嵌入式内容。 </returns>
public T? Resolve<T>(JsonTypeInfo<T> jsonTypeInfo)
where T : IEmbed
{
T? embed = JsonNode.Deserialize(jsonTypeInfo);
return embed;
}
/// <summary>
/// 通过指定的解析函数将嵌入式内容解析为具体类型。
/// </summary>
/// <param name="resolvingFunc"> 用于解析的函数。 </param>
/// <typeparam name="T"> 要解析为的具体类型。 </typeparam>
/// <returns> 解析后的嵌入式内容。 </returns>
public T Resolve<T>(Func<NotImplementedEmbed, T> resolvingFunc)
where T : IEmbed
{
T embed = resolvingFunc(this);
return embed;
}
}