diff --git a/src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs b/src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs index ae45bd310..1e2a7b0d7 100644 --- a/src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs +++ b/src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs @@ -164,35 +164,45 @@ namespace Discord /// if was succesfully parsed. if not. public static bool TryParse(string json, out EmbedBuilder builder) { - var model = JsonConvert.DeserializeObject(json); - builder = new EmbedBuilder(); - - if (model is not null) + try { - builder = model.ToEmbedBuilder(); - return true; - } + var model = JsonConvert.DeserializeObject(json); - else + 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 + /// An with populated values from the passed . /// Thrown if the string passed is not valid json. public static EmbedBuilder Parse(string json) { - var model = JsonConvert.DeserializeObject(json); + try + { + var model = JsonConvert.DeserializeObject(json); - if (model is not null) - return model.ToEmbedBuilder(); + if (model is not null) + return model.ToEmbedBuilder(); - else - throw new JsonSerializationException("The passed json string was not able to be parsed to an embed."); + return new EmbedBuilder(); + } + catch + { + throw; + } } /// diff --git a/src/Discord.Net.Rest/Extensions/StringExtensions.cs b/src/Discord.Net.Rest/Extensions/StringExtensions.cs index bc6e20ae8..7245a8325 100644 --- a/src/Discord.Net.Rest/Extensions/StringExtensions.cs +++ b/src/Discord.Net.Rest/Extensions/StringExtensions.cs @@ -1,5 +1,7 @@ using Discord.Net.Converters; using Newtonsoft.Json; +using System.Linq; +using System; namespace Discord.Rest { @@ -8,6 +10,16 @@ namespace Discord.Rest /// 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 . /// @@ -30,6 +42,6 @@ namespace Discord.Rest /// 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()); + => JsonConvert.SerializeObject(embed.ToModel(), formatting, _settings.Value); } }