[Fix] Embed ToJsonString & (Try)Parse (#2787)
* fix * EmbedBuilderUtils * lazy * xmldocs...
This commit is contained in:
@@ -160,55 +160,6 @@ namespace Discord
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Tries to parse a string into an <see cref="EmbedBuilder"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="json">The json string to parse.</param>
|
|
||||||
/// <param name="builder">The <see cref="EmbedBuilder"/> with populated values. An empty instance if method returns <see langword="false"/>.</param>
|
|
||||||
/// <returns><see langword="true"/> if <paramref name="json"/> was successfully parsed. <see langword="false"/> if not.</returns>
|
|
||||||
public static bool TryParse(string json, out EmbedBuilder builder)
|
|
||||||
{
|
|
||||||
builder = new EmbedBuilder();
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var model = JsonConvert.DeserializeObject<Embed>(json);
|
|
||||||
|
|
||||||
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>
|
|
||||||
/// <exception cref="InvalidOperationException">Thrown if the string passed is not valid json.</exception>
|
|
||||||
public static EmbedBuilder Parse(string json)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var model = JsonConvert.DeserializeObject<Embed>(json);
|
|
||||||
|
|
||||||
if (model is not null)
|
|
||||||
return model.ToEmbedBuilder();
|
|
||||||
|
|
||||||
return new EmbedBuilder();
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sets the title of an <see cref="Embed"/>.
|
/// Sets the title of an <see cref="Embed"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ namespace Discord.Rest
|
|||||||
{
|
{
|
||||||
ContractResolver = new DiscordContractResolver()
|
ContractResolver = new DiscordContractResolver()
|
||||||
};
|
};
|
||||||
serializer.Converters.Add(new EmbedTypeConverter());
|
|
||||||
return serializer;
|
return serializer;
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -24,7 +23,7 @@ namespace Discord.Rest
|
|||||||
/// Gets a Json formatted <see langword="string"/> from an <see cref="EmbedBuilder"/>.
|
/// Gets a Json formatted <see langword="string"/> from an <see cref="EmbedBuilder"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// See <see cref="EmbedBuilder.TryParse(string, out EmbedBuilder)"/> to parse Json back into embed.
|
/// See <see cref="EmbedBuilderUtils.TryParse(string, out EmbedBuilder)"/> to parse Json back into embed.
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
/// <param name="builder">The builder to format as Json <see langword="string"/>.</param>
|
/// <param name="builder">The builder to format as Json <see langword="string"/>.</param>
|
||||||
/// <param name="formatting">The formatting in which the Json will be returned.</param>
|
/// <param name="formatting">The formatting in which the Json will be returned.</param>
|
||||||
@@ -36,7 +35,7 @@ namespace Discord.Rest
|
|||||||
/// Gets a Json formatted <see langword="string"/> from an <see cref="Embed"/>.
|
/// Gets a Json formatted <see langword="string"/> from an <see cref="Embed"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// See <see cref="EmbedBuilder.TryParse(string, out EmbedBuilder)"/> to parse Json back into embed.
|
/// See <see cref="EmbedBuilderUtils.TryParse(string, out EmbedBuilder)"/> to parse Json back into embed.
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
/// <param name="embed">The embed to format as Json <see langword="string"/>.</param>
|
/// <param name="embed">The embed to format as Json <see langword="string"/>.</param>
|
||||||
/// <param name="formatting">The formatting in which the Json will be returned.</param>
|
/// <param name="formatting">The formatting in which the Json will be returned.</param>
|
||||||
|
|||||||
71
src/Discord.Net.Rest/Utils/EmbedBuilderUtils.cs
Normal file
71
src/Discord.Net.Rest/Utils/EmbedBuilderUtils.cs
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
using Discord.Net.Converters;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Discord.Rest;
|
||||||
|
|
||||||
|
public static class EmbedBuilderUtils
|
||||||
|
{
|
||||||
|
private static Lazy<JsonSerializerSettings> _settings = new(() =>
|
||||||
|
{
|
||||||
|
var serializer = new JsonSerializerSettings()
|
||||||
|
{
|
||||||
|
ContractResolver = new DiscordContractResolver()
|
||||||
|
};
|
||||||
|
return serializer;
|
||||||
|
});
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
/// <exception cref="InvalidOperationException">Thrown if the string passed is not valid json.</exception>
|
||||||
|
public static EmbedBuilder Parse(string json)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var model = JsonConvert.DeserializeObject<API.Embed>(json, _settings.Value);
|
||||||
|
|
||||||
|
var embed = model?.ToEntity();
|
||||||
|
|
||||||
|
if (embed is not null)
|
||||||
|
return embed.ToEmbedBuilder();
|
||||||
|
|
||||||
|
return new EmbedBuilder();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tries to parse a string into an <see cref="EmbedBuilder"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="json">The json string to parse.</param>
|
||||||
|
/// <param name="builder">The <see cref="EmbedBuilder"/> with populated values. An empty instance if method returns <see langword="false"/>.</param>
|
||||||
|
/// <returns><see langword="true"/> if <paramref name="json"/> was successfully parsed. <see langword="false"/> if not.</returns>
|
||||||
|
public static bool TryParse(string json, out EmbedBuilder builder)
|
||||||
|
{
|
||||||
|
builder = new EmbedBuilder();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var model = JsonConvert.DeserializeObject<API.Embed>(json, _settings.Value);
|
||||||
|
|
||||||
|
var embed = model?.ToEntity();
|
||||||
|
|
||||||
|
if (embed is not null)
|
||||||
|
{
|
||||||
|
builder = embed.ToEmbedBuilder();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user