microsoft/teams.net

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
docs/update-release-process

Branches

Tags

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

Clone

HTTPS

Download ZIP

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

70lines · 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(OMessageJsonConverter))]
10public class OMessageEntity : Entity, IMessageEntity
11{
12 [JsonPropertyName("additionalType")]
13 [JsonPropertyOrder(10)]
14 public IList<string>? AdditionalType { get; set; }
15
16 public OMessageEntity() : base("https://schema.org/Message")
17 {
18 OType = "Message";
19 OContext = "https://schema.org";
20 }
21
22 public class OMessageJsonConverter : JsonConverter<OMessageEntity>
23 {
24 public override bool CanConvert(Type typeToConvert)
25 {
26 return base.CanConvert(typeToConvert);
27 }
28
29 public override OMessageEntity? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
30 {
31 var element = JsonSerializer.Deserialize<JsonElement>(ref reader, options);
32
33 if (!element.TryGetProperty("@type", out JsonElement property))
34 {
35 throw new JsonException("'https://schema.org/Message' entity must have a '@type' property");
36 }
37
38 var oType = property.Deserialize<string>(options);
39
40 if (oType is null)
41 {
42 throw new JsonException("failed to deserialize 'https://schema.org/Message' entity '@type' property");
43 }
44
45 return oType switch
46 {
47 "Message" => JsonSerializer.Deserialize<CitationEntity>(element.ToString(), options),
48 "CreativeWork" => JsonSerializer.Deserialize<SensitiveUsageEntity>(element.ToString(), options),
49 _ => throw new JsonException($"failed to deserialize omessage entity '{oType}' doesn't match any known types.")
50 };
51 }
52
53 public override void Write(Utf8JsonWriter writer, OMessageEntity value, JsonSerializerOptions options)
54 {
55 if (value is CitationEntity citation)
56 {
57 JsonSerializer.Serialize(writer, citation, options);
58 return;
59 }
60
61 if (value is SensitiveUsageEntity sensitiveUsage)
62 {
63 JsonSerializer.Serialize(writer, sensitiveUsage, options);
64 return;
65 }
66
67 JsonSerializer.Serialize(writer, value, options);
68 }
69 }
70}