microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
Libraries/Microsoft.Teams.Api/Activities/EventActivity.JsonConverter.cs
80lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Text.Json; |
| 5 | using System.Text.Json.Serialization; |
| 6 | |
| 7 | namespace Microsoft.Teams.Api.Activities; |
| 8 | |
| 9 | public partial class EventActivity |
| 10 | { |
| 11 | public new class JsonConverter : JsonConverter<EventActivity> |
| 12 | { |
| 13 | public override bool CanConvert(Type typeToConvert) |
| 14 | { |
| 15 | return base.CanConvert(typeToConvert); |
| 16 | } |
| 17 | |
| 18 | public override EventActivity? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 19 | { |
| 20 | var element = JsonSerializer.Deserialize<JsonElement>(ref reader, options); |
| 21 | |
| 22 | if (!element.TryGetProperty("name", out JsonElement property)) |
| 23 | { |
| 24 | throw new JsonException("event activity must have a 'name' property"); |
| 25 | } |
| 26 | |
| 27 | var name = property.Deserialize<string>(options); |
| 28 | |
| 29 | if (name is null) |
| 30 | { |
| 31 | throw new JsonException("failed to deserialize event activity 'name' property"); |
| 32 | } |
| 33 | |
| 34 | return name switch |
| 35 | { |
| 36 | "application/vnd.microsoft.meetingEnd" => JsonSerializer.Deserialize<Events.MeetingEndActivity>(element.ToString(), options), |
| 37 | "application/vnd.microsoft.meetingStart" => JsonSerializer.Deserialize<Events.MeetingStartActivity>(element.ToString(), options), |
| 38 | "application/vnd.microsoft.meetingParticipantJoin" => JsonSerializer.Deserialize<Events.MeetingParticipantJoinActivity>(element.ToString(), options), |
| 39 | "application/vnd.microsoft.meetingParticipantLeave" => JsonSerializer.Deserialize<Events.MeetingParticipantLeaveActivity>(element.ToString(), options), |
| 40 | "application/vnd.microsoft.readReceipt" => JsonSerializer.Deserialize<Events.ReadReceiptActivity>(element.ToString(), options), |
| 41 | _ => new EventActivity(new(name)) |
| 42 | }; |
| 43 | } |
| 44 | |
| 45 | public override void Write(Utf8JsonWriter writer, EventActivity value, JsonSerializerOptions options) |
| 46 | { |
| 47 | if (value is Events.MeetingEndActivity meetingEnd) |
| 48 | { |
| 49 | JsonSerializer.Serialize(writer, meetingEnd, options); |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | if (value is Events.MeetingStartActivity meetingStart) |
| 54 | { |
| 55 | JsonSerializer.Serialize(writer, meetingStart, options); |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | if (value is Events.MeetingParticipantJoinActivity meetingParticipantJoin) |
| 60 | { |
| 61 | JsonSerializer.Serialize(writer, meetingParticipantJoin, options); |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | if (value is Events.MeetingParticipantLeaveActivity meetingParticipantLeave) |
| 66 | { |
| 67 | JsonSerializer.Serialize(writer, meetingParticipantLeave, options); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | if (value is Events.ReadReceiptActivity readReceipt) |
| 72 | { |
| 73 | JsonSerializer.Serialize(writer, readReceipt, options); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | JsonSerializer.Serialize(writer, value, value.GetType(), options); |
| 78 | } |
| 79 | } |
| 80 | } |