diff --git a/DisCatSharp/Net/Serialization/DiscordComponentJsonConverter.cs b/DisCatSharp/Net/Serialization/DiscordComponentJsonConverter.cs index 7e22aba1c..0e4910ebb 100644 --- a/DisCatSharp/Net/Serialization/DiscordComponentJsonConverter.cs +++ b/DisCatSharp/Net/Serialization/DiscordComponentJsonConverter.cs @@ -1,100 +1,100 @@ // This file is part of the DisCatSharp project. // // Copyright (c) 2021 AITSYS // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. using System; using DisCatSharp.Entities; using DisCatSharp.Enums; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace DisCatSharp.Net.Serialization { /// /// Represents a discord component json converter. /// internal sealed class DiscordComponentJsonConverter : JsonConverter { /// /// Whether the converter can write. /// public override bool CanWrite => false; /// /// Writes the json. /// /// The writer. /// The value. /// The serializer. public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) => throw new NotImplementedException(); /// /// Reads the json. /// /// The reader. /// The object type. /// The existing value. /// The serializer. public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Null) return null; var job = JObject.Load(reader); var type = job["type"]?.ToObject(); - var style = job["style"]?.ToObject(); if (type == null) throw new ArgumentException($"Value {reader} does not have a component type specifier"); DiscordComponent cmp; - if (type is ComponentType.Button && style is not null) + if (type is ComponentType.Button) { + var style = job["style"]?.ToObject(); cmp = style switch { ButtonStyle.Link => new DiscordLinkButtonComponent(), _ => new DiscordButtonComponent() }; } else { cmp = type switch { ComponentType.ActionRow => new DiscordActionRowComponent(), ComponentType.Select => new DiscordSelectComponent(), ComponentType.InputText => new DiscordTextComponent(), _ => new DiscordComponent() { Type = type.Value } }; } // Populate the existing component with the values in the JObject. This avoids a recursive JsonConverter loop using var jreader = job.CreateReader(); serializer.Populate(jreader, cmp); return cmp; } /// /// Whether the json can convert. /// /// The object type. public override bool CanConvert(Type objectType) => typeof(DiscordComponent).IsAssignableFrom(objectType); } }