diff --git a/src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs b/src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs index 0304120f5..1e2a7b0d7 100644 --- a/src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs +++ b/src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using Discord.Utils; +using Newtonsoft.Json; namespace Discord { @@ -155,6 +156,55 @@ 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 succesfully 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 new file mode 100644 index 000000000..4981a4298 --- /dev/null +++ b/src/Discord.Net.Rest/Extensions/StringExtensions.cs @@ -0,0 +1,47 @@ +using Discord.Net.Converters; +using Newtonsoft.Json; +using System.Linq; +using System; + +namespace Discord.Rest +{ + /// + /// Responsible for formatting certain entities as Json , to reuse later on. + /// + public static class StringExtensions + { + private static Lazy _settings = new(() => + { + var serializer = new JsonSerializerSettings() + { + ContractResolver = new DiscordContractResolver() + }; + serializer.Converters.Add(new EmbedTypeConverter()); + return serializer; + }); + + /// + /// Gets a Json formatted from an . + /// + /// + /// See to parse Json back into embed. + /// + /// 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 . + /// + /// + /// 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, _settings.Value); + } +}