diff --git a/src/NadekoBot/Modules/Searches/Common/LowerCaseNamingPolicy.cs b/src/NadekoBot/Modules/Searches/Common/LowerCaseNamingPolicy.cs new file mode 100644 index 000000000..91b6807b8 --- /dev/null +++ b/src/NadekoBot/Modules/Searches/Common/LowerCaseNamingPolicy.cs @@ -0,0 +1,12 @@ +using System.Text.Json; + +namespace SystemTextJsonSamples +{ + public class LowerCaseNamingPolicy : JsonNamingPolicy + { + public static LowerCaseNamingPolicy Default = new LowerCaseNamingPolicy(); + + public override string ConvertName(string name) => + name.ToLower(); + } +} \ No newline at end of file diff --git a/src/NadekoBot/Modules/Utility/Utility.cs b/src/NadekoBot/Modules/Utility/Utility.cs index 4cf0f3ef6..017da97b8 100644 --- a/src/NadekoBot/Modules/Utility/Utility.cs +++ b/src/NadekoBot/Modules/Utility/Utility.cs @@ -11,11 +11,14 @@ using System.Diagnostics; using System.Linq; using System.Net.Http; using System.Text; +using System.Text.Json; using System.Threading; using System.Threading.Tasks; using NadekoBot.Common.Replacements; using NadekoBot.Services; using Serilog; +using SystemTextJsonSamples; +using JsonSerializer = Newtonsoft.Json.JsonSerializer; namespace NadekoBot.Modules.Utility { @@ -368,6 +371,13 @@ namespace NadekoBot.Modules.Utility public Task ShowEmbed(ulong messageId) => ShowEmbed((ITextChannel)ctx.Channel, messageId); + private static readonly JsonSerializerOptions _showEmbedSerializerOptions = new JsonSerializerOptions() + { + WriteIndented = true, + IgnoreNullValues = true, + PropertyNamingPolicy = LowerCaseNamingPolicy.Default + }; + [NadekoCommand, Aliases] [RequireContext(ContextType.Guild)] public async Task ShowEmbed(ITextChannel ch, ulong messageId) @@ -394,7 +404,7 @@ namespace NadekoBot.Modules.Utility return; } - var json = SmartEmbedText.FromEmbed(embed, msg.Content).ToJson(); + var json = SmartEmbedText.FromEmbed(embed, msg.Content).ToJson(_showEmbedSerializerOptions); await SendConfirmAsync(Format.Sanitize(json).Replace("](", "]\\(")); } diff --git a/src/NadekoBot/_Extensions/Extensions.cs b/src/NadekoBot/_Extensions/Extensions.cs index 2c1ca8537..051b82ff2 100644 --- a/src/NadekoBot/_Extensions/Extensions.cs +++ b/src/NadekoBot/_Extensions/Extensions.cs @@ -22,10 +22,12 @@ using System.Linq; using System.Net.Http; using System.Net.Http.Headers; using System.Text; +using System.Text.Json; using System.Text.RegularExpressions; using System.Threading.Tasks; using NadekoBot.Common.Attributes; using Color = Discord.Color; +using JsonSerializer = System.Text.Json.JsonSerializer; namespace NadekoBot.Extensions { @@ -286,8 +288,8 @@ namespace NadekoBot.Extensions public static async Task> GetMembersAsync(this IRole role) => (await role.Guild.GetUsersAsync(CacheMode.CacheOnly).ConfigureAwait(false)).Where(u => u.RoleIds.Contains(role.Id)) ?? Enumerable.Empty(); - public static string ToJson(this T any, Formatting formatting = Formatting.Indented) => - JsonConvert.SerializeObject(any, formatting); + public static string ToJson(this T any, JsonSerializerOptions options = null) => + JsonSerializer.Serialize(any, options); /// /// Adds fallback fonts to