microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Libraries/Microsoft.Teams.Api/Activities/Activity.JsonConverter.cs
169lines · 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.Nodes; |
| 6 | using System.Text.Json.Serialization; |
| 7 | |
| 8 | using Microsoft.Teams.Common.Json; |
| 9 | |
| 10 | namespace Microsoft.Teams.Api.Activities; |
| 11 | |
| 12 | public partial interface IActivity |
| 13 | { |
| 14 | public class JsonConverter : JsonConverter<IActivity> |
| 15 | { |
| 16 | public override bool CanConvert(Type typeToConvert) |
| 17 | { |
| 18 | return base.CanConvert(typeToConvert); |
| 19 | } |
| 20 | |
| 21 | public override IActivity? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 22 | { |
| 23 | return JsonSerializer.Deserialize<Activity>(ref reader, options); |
| 24 | } |
| 25 | |
| 26 | public override void Write(Utf8JsonWriter writer, IActivity value, JsonSerializerOptions options) |
| 27 | { |
| 28 | // default to the underlying class type to avoid recursive serialization |
| 29 | JsonSerializer.Serialize(writer, value, value.GetType(), options); |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | public partial class Activity |
| 35 | { |
| 36 | public class JsonConverter : JsonConverter<Activity> |
| 37 | { |
| 38 | public override bool CanConvert(Type typeToConvert) |
| 39 | { |
| 40 | return base.CanConvert(typeToConvert); |
| 41 | } |
| 42 | |
| 43 | public override Activity? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 44 | { |
| 45 | var element = JsonSerializer.Deserialize<JsonObject>(ref reader, options) ?? throw new Exception("expected json object"); |
| 46 | |
| 47 | if (!element.TryGetPropertyValue("type", out var typeNode)) |
| 48 | { |
| 49 | throw new JsonException("activity must have a 'type' property"); |
| 50 | } |
| 51 | |
| 52 | var type = typeNode.Deserialize<string>(options); |
| 53 | |
| 54 | if (type is null) |
| 55 | { |
| 56 | throw new JsonException("failed to deserialize activity 'type' property"); |
| 57 | } |
| 58 | |
| 59 | Activity? activity = type switch |
| 60 | { |
| 61 | "typing" => element.Deserialize<TypingActivity>(options), |
| 62 | "message" => element.Deserialize<MessageActivity>(options), |
| 63 | "messageUpdate" => element.Deserialize<MessageUpdateActivity>(options), |
| 64 | "messageDelete" => element.Deserialize<MessageDeleteActivity>(options), |
| 65 | "messageReaction" => element.Deserialize<MessageReactionActivity>(options), |
| 66 | "conversationUpdate" => element.Deserialize<ConversationUpdateActivity>(options), |
| 67 | "endOfConversation" => element.Deserialize<EndOfConversationActivity>(options), |
| 68 | "installationUpdate" => element.Deserialize<InstallUpdateActivity>(options), |
| 69 | "command" => element.Deserialize<CommandActivity>(options), |
| 70 | "commandResult" => element.Deserialize<CommandResultActivity>(options), |
| 71 | "event" => element.Deserialize<EventActivity>(options), |
| 72 | "invoke" => element.Deserialize<InvokeActivity>(options), |
| 73 | _ => null |
| 74 | }; |
| 75 | |
| 76 | if (activity is null) |
| 77 | { |
| 78 | activity = new(type); |
| 79 | activity.Properties = activity.FromJsonObject(element, options); |
| 80 | } |
| 81 | |
| 82 | return activity; |
| 83 | } |
| 84 | |
| 85 | public override void Write(Utf8JsonWriter writer, Activity value, JsonSerializerOptions options) |
| 86 | { |
| 87 | if (value is TypingActivity typing) |
| 88 | { |
| 89 | JsonSerializer.Serialize(writer, typing, options); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | if (value is MessageActivity message) |
| 94 | { |
| 95 | JsonSerializer.Serialize(writer, message, options); |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | if (value is MessageUpdateActivity messageUpdate) |
| 100 | { |
| 101 | JsonSerializer.Serialize(writer, messageUpdate, options); |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | if (value is MessageDeleteActivity messageDelete) |
| 106 | { |
| 107 | JsonSerializer.Serialize(writer, messageDelete, options); |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | if (value is MessageReactionActivity messageReaction) |
| 112 | { |
| 113 | JsonSerializer.Serialize(writer, messageReaction, options); |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | if (value is ConversationUpdateActivity conversationUpdate) |
| 118 | { |
| 119 | JsonSerializer.Serialize(writer, conversationUpdate, options); |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | if (value is EndOfConversationActivity endOfConversation) |
| 124 | { |
| 125 | JsonSerializer.Serialize(writer, endOfConversation, options); |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | if (value is CommandActivity command) |
| 130 | { |
| 131 | JsonSerializer.Serialize(writer, command, options); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | if (value is CommandResultActivity commandResult) |
| 136 | { |
| 137 | JsonSerializer.Serialize(writer, commandResult, options); |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | if (value is EventActivity @event) |
| 142 | { |
| 143 | JsonSerializer.Serialize(writer, @event, options); |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | if (value is InvokeActivity invoke) |
| 148 | { |
| 149 | JsonSerializer.Serialize(writer, invoke, options); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | JsonSerializer.Serialize(writer, value.ToJsonObject(options), options); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | public class ActivityJsonConverter : JsonConverterFactory |
| 159 | { |
| 160 | public override bool CanConvert(Type type) |
| 161 | { |
| 162 | return typeof(IActivity).IsAssignableFrom(type); |
| 163 | } |
| 164 | |
| 165 | public override JsonConverter? CreateConverter(Type type, JsonSerializerOptions options) |
| 166 | { |
| 167 | return type == typeof(Activity) ? new Activity.JsonConverter() : new IActivity.JsonConverter(); |
| 168 | } |
| 169 | } |