From 26841e63d81decc63d713d3d3e0856c74fe371ab Mon Sep 17 00:00:00 2001 From: Armano den Boef Date: Tue, 3 May 2022 13:32:52 +0200 Subject: [PATCH] Add tostring extension for embeds --- .../Extensions/StringExtensions.cs | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/Discord.Net.Rest/Extensions/StringExtensions.cs diff --git a/src/Discord.Net.Rest/Extensions/StringExtensions.cs b/src/Discord.Net.Rest/Extensions/StringExtensions.cs new file mode 100644 index 000000000..9768b27e4 --- /dev/null +++ b/src/Discord.Net.Rest/Extensions/StringExtensions.cs @@ -0,0 +1,31 @@ +using Discord.Net.Converters; +using Newtonsoft.Json; + +namespace Discord.Rest +{ + /// + /// Responsible for formatting certain entities as json string, to reuse later on. + /// + public static class StringExtensions + { + /// + /// Gets a Json formatted from an to reuse. + /// See to parse Json back into embeds. + /// + /// The builder to format as Json . + /// The formatting in which the Json will be returned. + /// A Json containing the data from the . + public static string ToJsonString(this EmbedBuilder builder, Formatting formatting = Formatting.Indented) + => ToJsonString(builder.Build(), formatting); + + /// + /// Gets a Json formatted from an to reuse. + /// See to parse Json back into embed. + /// + /// The embed to format as Json . + /// The formatting in which the Json will be returned. + /// A Json containing the data from the . + public static string ToJsonString(this Embed embed, Formatting formatting = Formatting.Indented) + => JsonConvert.SerializeObject(embed.ToModel(), formatting, new EmbedTypeConverter()); + } +}