microsoft/teams.net

Public

mirrored from https://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
pr-134

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

Libraries/Microsoft.Teams.Api/Entities/Entity.cs

183lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4using System.Text.Json;
5using System.Text.Json.Serialization;
6
7namespace Microsoft.Teams.Api.Entities;
8
9[JsonConverter(typeof(JsonConverter))]
10public interface IEntity
11{
12 [JsonPropertyName("type")]
13 [JsonPropertyOrder(0)]
14 public string Type { get; set; }
15
16 [JsonPropertyName("@type")]
17 [JsonPropertyOrder(1)]
18 public string? OType { get; set; }
19
20 [JsonPropertyName("@context")]
21 [JsonPropertyOrder(2)]
22 public string? OContext { get; set; }
23
24 [JsonExtensionData]
25 public IDictionary<string, object?> Properties { get; set; }
26
27 public class JsonConverter : JsonConverter<IEntity>
28 {
29 public override bool CanConvert(Type typeToConvert)
30 {
31 return base.CanConvert(typeToConvert);
32 }
33
34 public override IEntity? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
35 {
36 var element = JsonSerializer.Deserialize<JsonElement>(ref reader, options);
37
38 if (!element.TryGetProperty("type", out JsonElement property))
39 {
40 throw new JsonException("entity must have a 'type' property");
41 }
42
43 var type = property.Deserialize<string>(options);
44
45 if (type is null)
46 {
47 throw new JsonException("failed to deserialize entity 'type' property");
48 }
49
50 return type switch
51 {
52 "clientInfo" => JsonSerializer.Deserialize<ClientInfoEntity>(element.ToString(), options),
53 "mention" => JsonSerializer.Deserialize<MentionEntity>(element.ToString(), options),
54 "message" or "https://schema.org/Message" => JsonSerializer.Deserialize<IMessageEntity>(element.ToString(), options),
55 "streaminfo" => JsonSerializer.Deserialize<StreamInfoEntity>(element.ToString(), options),
56 _ => JsonSerializer.Deserialize<Entity>(element.ToString(), options)
57 };
58 }
59
60 public override void Write(Utf8JsonWriter writer, IEntity value, JsonSerializerOptions options)
61 {
62 if (value is ClientInfoEntity clientInfo)
63 {
64 JsonSerializer.Serialize(writer, clientInfo, options);
65 return;
66 }
67
68 if (value is MentionEntity mention)
69 {
70 JsonSerializer.Serialize(writer, mention, options);
71 return;
72 }
73
74 if (value is IMessageEntity message)
75 {
76 JsonSerializer.Serialize(writer, message, options);
77 return;
78 }
79
80 if (value is StreamInfoEntity streamInfo)
81 {
82 JsonSerializer.Serialize(writer, streamInfo, options);
83 return;
84 }
85
86 JsonSerializer.Serialize(writer, value, options);
87 }
88 }
89}
90
91[JsonConverter(typeof(JsonConverter))]
92public class Entity : IEntity
93{
94 [JsonPropertyName("type")]
95 [JsonPropertyOrder(0)]
96 public string Type { get; set; }
97
98 [JsonPropertyName("@type")]
99 [JsonPropertyOrder(1)]
100 public string? OType { get; set; }
101
102 [JsonPropertyName("@context")]
103 [JsonPropertyOrder(2)]
104 public string? OContext { get; set; }
105
106 [JsonExtensionData]
107 public IDictionary<string, object?> Properties { get; set; } = new Dictionary<string, object?>();
108
109 [JsonConstructor]
110 public Entity(string type)
111 {
112 Type = type;
113 }
114
115 public Entity(string type, string? otype)
116 {
117 Type = type;
118 OType = otype;
119 }
120
121 public class JsonConverter : JsonConverter<Entity>
122 {
123 public override bool CanConvert(Type typeToConvert)
124 {
125 return base.CanConvert(typeToConvert);
126 }
127
128 public override Entity? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
129 {
130 var element = JsonSerializer.Deserialize<JsonElement>(ref reader, options);
131
132 if (!element.TryGetProperty("type", out JsonElement property))
133 {
134 throw new JsonException("entity must have a 'type' property");
135 }
136
137 var type = property.Deserialize<string>(options);
138
139 if (type is null)
140 {
141 throw new JsonException("failed to deserialize entity 'type' property");
142 }
143
144 return type switch
145 {
146 "clientInfo" => JsonSerializer.Deserialize<ClientInfoEntity>(element.ToString(), options),
147 "mention" => JsonSerializer.Deserialize<MentionEntity>(element.ToString(), options),
148 "message" or "https://schema.org/Message" => (Entity?)JsonSerializer.Deserialize<IMessageEntity>(element.ToString(), options),
149 "streaminfo" => JsonSerializer.Deserialize<StreamInfoEntity>(element.ToString(), options),
150 _ => JsonSerializer.Deserialize<Entity>(element.ToString(), options)
151 };
152 }
153
154 public override void Write(Utf8JsonWriter writer, Entity value, JsonSerializerOptions options)
155 {
156 if (value is ClientInfoEntity clientInfo)
157 {
158 JsonSerializer.Serialize(writer, clientInfo, options);
159 return;
160 }
161
162 if (value is MentionEntity mention)
163 {
164 JsonSerializer.Serialize(writer, mention, options);
165 return;
166 }
167
168 if (value is IMessageEntity message)
169 {
170 JsonSerializer.Serialize(writer, message, options);
171 return;
172 }
173
174 if (value is StreamInfoEntity streamInfo)
175 {
176 JsonSerializer.Serialize(writer, streamInfo, options);
177 return;
178 }
179
180 JsonSerializer.Serialize(writer, value, options);
181 }
182 }
183}