microsoft/teams.net

Public

mirrored fromhttps://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
next/core-activitybuilder

Branches

Tags

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

Clone

HTTPS

Download ZIP

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

180lines · modecode

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