Browse Source

Resolve suggestions

pull/2284/head
Armano den Boef 3 years ago
parent
commit
e0ade1412d
2 changed files with 37 additions and 15 deletions
  1. +24
    -14
      src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs
  2. +13
    -1
      src/Discord.Net.Rest/Extensions/StringExtensions.cs

+ 24
- 14
src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs View File

@@ -164,35 +164,45 @@ namespace Discord
/// <returns><see langword="true"/> if <paramref name="json"/> was succesfully parsed. <see langword="false"/> if not.</returns>
public static bool TryParse(string json, out EmbedBuilder builder)
{
var model = JsonConvert.DeserializeObject<Embed>(json);

builder = new EmbedBuilder();

if (model is not null)
try
{
builder = model.ToEmbedBuilder();
return true;
}
var model = JsonConvert.DeserializeObject<Embed>(json);

else
if (model is not null)
{
builder = model.ToEmbedBuilder();
return true;
}
return false;
}
catch
{
return false;
}
}

/// <summary>
/// Parses a string into an <see cref="EmbedBuilder"/>.
/// </summary>
/// <param name="json">The json string to parse.</param>
/// <returns>An <see cref="EmbedBuilder"/> with populated values from the passed <paramref name="json"/></returns>
/// <returns>An <see cref="EmbedBuilder"/> with populated values from the passed <paramref name="json"/>.</returns>
/// <exception cref="InvalidOperationException">Thrown if the string passed is not valid json.</exception>
public static EmbedBuilder Parse(string json)
{
var model = JsonConvert.DeserializeObject<Embed>(json);
try
{
var model = JsonConvert.DeserializeObject<Embed>(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;
}
}

/// <summary>


+ 13
- 1
src/Discord.Net.Rest/Extensions/StringExtensions.cs View File

@@ -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
/// </summary>
public static class StringExtensions
{
private static Lazy<JsonSerializerSettings> _settings = new(() =>
{
var serializer = new JsonSerializerSettings()
{
ContractResolver = new DiscordContractResolver()
};
serializer.Converters.Add(new EmbedTypeConverter());
return serializer;
});

/// <summary>
/// Gets a Json formatted <see langword="string"/> from an <see cref="EmbedBuilder"/>.
/// </summary>
@@ -30,6 +42,6 @@ namespace Discord.Rest
/// <param name="formatting">The formatting in which the Json will be returned.</param>
/// <returns>A Json <see langword="string"/> containing the data from the <paramref name="builder"/>.</returns>
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);
}
}

Loading…
Cancel
Save