From 7b5c40aab64814256406f41496f495b3ceba9847 Mon Sep 17 00:00:00 2001 From: Mihail Gribkov <61027276+Misha-133@users.noreply.github.com> Date: Thu, 5 Oct 2023 19:10:01 +0300 Subject: [PATCH] [Fix] Embed `ToJsonString` & `(Try)Parse` (#2787) * fix * EmbedBuilderUtils * lazy * xmldocs... --- .../Entities/Messages/EmbedBuilder.cs | 49 ------------- .../Extensions/StringExtensions.cs | 5 +- .../Utils/EmbedBuilderUtils.cs | 71 +++++++++++++++++++ 3 files changed, 73 insertions(+), 52 deletions(-) create mode 100644 src/Discord.Net.Rest/Utils/EmbedBuilderUtils.cs diff --git a/src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs b/src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs index 7f049383..56301f46 100644 --- a/src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs +++ b/src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs @@ -160,55 +160,6 @@ namespace Discord } } - /// - /// Tries to parse a string into an . - /// - /// The json string to parse. - /// The with populated values. An empty instance if method returns . - /// if was successfully parsed. if not. - public static bool TryParse(string json, out EmbedBuilder builder) - { - builder = new EmbedBuilder(); - try - { - var model = JsonConvert.DeserializeObject(json); - - if (model is not null) - { - builder = model.ToEmbedBuilder(); - return true; - } - return false; - } - catch - { - return false; - } - } - - /// - /// Parses a string into an . - /// - /// The json string to parse. - /// An with populated values from the passed . - /// Thrown if the string passed is not valid json. - public static EmbedBuilder Parse(string json) - { - try - { - var model = JsonConvert.DeserializeObject(json); - - if (model is not null) - return model.ToEmbedBuilder(); - - return new EmbedBuilder(); - } - catch - { - throw; - } - } - /// /// Sets the title of an . /// diff --git a/src/Discord.Net.Rest/Extensions/StringExtensions.cs b/src/Discord.Net.Rest/Extensions/StringExtensions.cs index bc784d49..d39d439e 100644 --- a/src/Discord.Net.Rest/Extensions/StringExtensions.cs +++ b/src/Discord.Net.Rest/Extensions/StringExtensions.cs @@ -16,7 +16,6 @@ namespace Discord.Rest { ContractResolver = new DiscordContractResolver() }; - serializer.Converters.Add(new EmbedTypeConverter()); return serializer; }); @@ -24,7 +23,7 @@ namespace Discord.Rest /// Gets a Json formatted from an . /// /// - /// See to parse Json back into embed. + /// See to parse Json back into embed. /// /// The builder to format as Json . /// The formatting in which the Json will be returned. @@ -36,7 +35,7 @@ namespace Discord.Rest /// Gets a Json formatted from an . /// /// - /// See to parse Json back into embed. + /// See to parse Json back into embed. /// /// The embed to format as Json . /// The formatting in which the Json will be returned. diff --git a/src/Discord.Net.Rest/Utils/EmbedBuilderUtils.cs b/src/Discord.Net.Rest/Utils/EmbedBuilderUtils.cs new file mode 100644 index 00000000..511aa0ab --- /dev/null +++ b/src/Discord.Net.Rest/Utils/EmbedBuilderUtils.cs @@ -0,0 +1,71 @@ +using Discord.Net.Converters; +using Newtonsoft.Json; + +using System; + +namespace Discord.Rest; + +public static class EmbedBuilderUtils +{ + private static Lazy _settings = new(() => + { + var serializer = new JsonSerializerSettings() + { + ContractResolver = new DiscordContractResolver() + }; + return serializer; + }); + + /// + /// Parses a string into an . + /// + /// The json string to parse. + /// An with populated values from the passed . + /// Thrown if the string passed is not valid json. + public static EmbedBuilder Parse(string json) + { + try + { + var model = JsonConvert.DeserializeObject(json, _settings.Value); + + var embed = model?.ToEntity(); + + if (embed is not null) + return embed.ToEmbedBuilder(); + + return new EmbedBuilder(); + } + catch + { + throw; + } + } + + /// + /// Tries to parse a string into an . + /// + /// The json string to parse. + /// The with populated values. An empty instance if method returns . + /// if was successfully parsed. if not. + public static bool TryParse(string json, out EmbedBuilder builder) + { + builder = new EmbedBuilder(); + try + { + var model = JsonConvert.DeserializeObject(json, _settings.Value); + + var embed = model?.ToEntity(); + + if (embed is not null) + { + builder = embed.ToEmbedBuilder(); + return true; + } + return false; + } + catch + { + return false; + } + } +}