Fully remove the dependency on Newtonsoft.Json

This commit is contained in:
Daan Boerlage 2021-11-01 01:27:04 +01:00
parent cf0cd743b8
commit 8c2eabfd21
Signed by: daan
GPG key ID: FCE070E1E4956606
18 changed files with 92 additions and 22 deletions

View file

@ -46,9 +46,9 @@ namespace Geekbot.Bot.Commands.Integrations.LolMmr
var sb = new StringBuilder(); var sb = new StringBuilder();
sb.AppendLine($"**MMR for {summonerName}**"); sb.AppendLine($"**MMR for {summonerName}**");
sb.AppendLine($"Normal: {data.Normal.Avg}"); sb.AppendLine($"Normal: {data.Normal?.Avg ?? 0}");
sb.AppendLine($"Ranked: {data.Ranked.Avg}"); sb.AppendLine($"Ranked: {data.Ranked?.Avg ?? 0}");
sb.AppendLine($"ARAM: {data.ARAM.Avg}"); sb.AppendLine($"ARAM: {data.ARAM?.Avg ?? 0}");
await Context.Channel.SendMessageAsync(sb.ToString()); await Context.Channel.SendMessageAsync(sb.ToString());
} }

View file

@ -1,9 +1,16 @@
using System.Text.Json.Serialization;
namespace Geekbot.Bot.Commands.Integrations.LolMmr namespace Geekbot.Bot.Commands.Integrations.LolMmr
{ {
public class LolMmrDto public class LolMmrDto
{ {
[JsonPropertyName("ranked")]
public LolMrrInfoDto Ranked { get; set; } public LolMrrInfoDto Ranked { get; set; }
[JsonPropertyName("normal")]
public LolMrrInfoDto Normal { get; set; } public LolMrrInfoDto Normal { get; set; }
[JsonPropertyName("aram")]
public LolMrrInfoDto ARAM { get; set; } public LolMrrInfoDto ARAM { get; set; }
} }
} }

View file

@ -5,7 +5,6 @@ namespace Geekbot.Bot.Commands.Integrations.LolMmr
public class LolMrrInfoDto public class LolMrrInfoDto
{ {
[JsonPropertyName("avg")] [JsonPropertyName("avg")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public decimal? Avg { get; set; }
public decimal Avg { get; set; } = 0;
} }
} }

View file

@ -1,12 +1,26 @@
namespace Geekbot.Bot.Commands.Integrations.UbranDictionary using System.Text.Json.Serialization;
namespace Geekbot.Bot.Commands.Integrations.UbranDictionary
{ {
internal class UrbanListItemDto internal class UrbanListItemDto
{ {
[JsonPropertyName("definition")]
public string Definition { get; set; } public string Definition { get; set; }
[JsonPropertyName("permalink")]
public string Permalink { get; set; } public string Permalink { get; set; }
public string ThumbsUp { get; set; }
[JsonPropertyName("thumbs_up")]
public int ThumbsUp { get; set; }
[JsonPropertyName("word")]
public string Word { get; set; } public string Word { get; set; }
[JsonPropertyName("example")]
public string Example { get; set; } public string Example { get; set; }
public string ThumbsDown { get; set; }
[JsonPropertyName("thumbs_down")]
public int ThumbsDown { get; set; }
} }
} }

View file

@ -1,10 +1,14 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Text.Json.Serialization;
namespace Geekbot.Bot.Commands.Integrations.UbranDictionary namespace Geekbot.Bot.Commands.Integrations.UbranDictionary
{ {
internal class UrbanResponseDto internal class UrbanResponseDto
{ {
[JsonPropertyName("tags")]
public string[] Tags { get; set; } public string[] Tags { get; set; }
[JsonPropertyName("list")]
public List<UrbanListItemDto> List { get; set; } public List<UrbanListItemDto> List { get; set; }
} }
} }

View file

@ -45,8 +45,8 @@ namespace Geekbot.Bot.Commands.Integrations.UbranDictionary
if (!string.IsNullOrEmpty(definition.Definition)) eb.Description = ShortenIfToLong(definition.Definition, 1800); if (!string.IsNullOrEmpty(definition.Definition)) eb.Description = ShortenIfToLong(definition.Definition, 1800);
if (!string.IsNullOrEmpty(definition.Example)) eb.AddField("Example", ShortenIfToLong(definition.Example, 1024)); if (!string.IsNullOrEmpty(definition.Example)) eb.AddField("Example", ShortenIfToLong(definition.Example, 1024));
if (!string.IsNullOrEmpty(definition.ThumbsUp)) eb.AddInlineField("Upvotes", definition.ThumbsUp); if (definition.ThumbsUp != 0) eb.AddInlineField("Upvotes", definition.ThumbsUp);
if (!string.IsNullOrEmpty(definition.ThumbsDown)) eb.AddInlineField("Downvotes", definition.ThumbsDown); if (definition.ThumbsDown != 0) eb.AddInlineField("Downvotes", definition.ThumbsDown);
if (definitions.Tags?.Length > 0) eb.AddField("Tags", string.Join(", ", definitions.Tags)); if (definitions.Tags?.Length > 0) eb.AddField("Tags", string.Join(", ", definitions.Tags));
await ReplyAsync("", false, eb.Build()); await ReplyAsync("", false, eb.Build());

View file

@ -1,7 +1,10 @@
namespace Geekbot.Bot.Commands.Randomness.Cat using System.Text.Json.Serialization;
namespace Geekbot.Bot.Commands.Randomness.Cat
{ {
internal class CatResponseDto internal class CatResponseDto
{ {
[JsonPropertyName("file")]
public string File { get; set; } public string File { get; set; }
} }
} }

View file

@ -1,7 +1,10 @@
namespace Geekbot.Bot.Commands.Randomness.Chuck using System.Text.Json.Serialization;
namespace Geekbot.Bot.Commands.Randomness.Chuck
{ {
internal class ChuckNorrisJokeResponseDto internal class ChuckNorrisJokeResponseDto
{ {
[JsonPropertyName("value")]
public string Value { get; set; } public string Value { get; set; }
} }
} }

View file

@ -1,7 +1,10 @@
namespace Geekbot.Bot.Commands.Randomness.Dad using System.Text.Json.Serialization;
namespace Geekbot.Bot.Commands.Randomness.Dad
{ {
internal class DadJokeResponseDto internal class DadJokeResponseDto
{ {
[JsonPropertyName("joke")]
public string Joke { get; set; } public string Joke { get; set; }
} }
} }

View file

@ -1,7 +1,10 @@
namespace Geekbot.Bot.Commands.Randomness.Dog using System.Text.Json.Serialization;
namespace Geekbot.Bot.Commands.Randomness.Dog
{ {
internal class DogResponseDto internal class DogResponseDto
{ {
[JsonPropertyName("url")]
public string Url { get; set; } public string Url { get; set; }
} }
} }

View file

@ -1,11 +1,22 @@
using System.Text.Json.Serialization;
namespace Geekbot.Bot.Commands.Randomness.Greetings namespace Geekbot.Bot.Commands.Randomness.Greetings
{ {
public class GreetingBaseDto public class GreetingBaseDto
{ {
[JsonPropertyName("language")]
public string Language { get; set; } public string Language { get; set; }
[JsonPropertyName("languageNative")]
public string LanguageNative { get; set; } public string LanguageNative { get; set; }
[JsonPropertyName("languageCode")]
public string LanguageCode { get; set; } public string LanguageCode { get; set; }
[JsonPropertyName("script")]
public string Script { get; set; } public string Script { get; set; }
[JsonPropertyName("primary")]
public GreetingDto Primary { get; set; } public GreetingDto Primary { get; set; }
} }
} }

View file

@ -1,10 +1,19 @@
using System.Text.Json.Serialization;
namespace Geekbot.Bot.Commands.Randomness.Greetings namespace Geekbot.Bot.Commands.Randomness.Greetings
{ {
public class GreetingDto public class GreetingDto
{ {
[JsonPropertyName("text")]
public string Text { get; set; } public string Text { get; set; }
[JsonPropertyName("dialect")]
public string Dialect { get; set; } public string Dialect { get; set; }
[JsonPropertyName("romanization")]
public string Romanization { get; set; } public string Romanization { get; set; }
[JsonPropertyName("use")]
public string[] Use { get; set; } public string[] Use { get; set; }
} }
} }

View file

@ -1,8 +1,10 @@
using System.Text.Json.Serialization;
namespace Geekbot.Bot.Commands.Randomness.Kanye namespace Geekbot.Bot.Commands.Randomness.Kanye
{ {
public class KanyeResponseDto public class KanyeResponseDto
{ {
public string Id { get; set; } [JsonPropertyName("quote")]
public string Quote { get; set; } public string Quote { get; set; }
} }
} }

View file

@ -1,11 +1,17 @@
using System; using System;
using System.Text.Json.Serialization;
namespace Geekbot.Bot.Commands.Utils.Changelog namespace Geekbot.Bot.Commands.Utils.Changelog
{ {
public class CommitAuthorDto public class CommitAuthorDto
{ {
[JsonPropertyName("name")]
public string Name { get; set; } public string Name { get; set; }
[JsonPropertyName("email")]
public string Email { get; set; } public string Email { get; set; }
[JsonPropertyName("date")]
public DateTimeOffset Date { get; set; } public DateTimeOffset Date { get; set; }
} }
} }

View file

@ -1,7 +1,10 @@
namespace Geekbot.Bot.Commands.Utils.Changelog using System.Text.Json.Serialization;
namespace Geekbot.Bot.Commands.Utils.Changelog
{ {
public class CommitDto public class CommitDto
{ {
[JsonPropertyName("commit")]
public CommitInfoDto Commit { get; set; } public CommitInfoDto Commit { get; set; }
} }
} }

View file

@ -1,8 +1,13 @@
namespace Geekbot.Bot.Commands.Utils.Changelog using System.Text.Json.Serialization;
namespace Geekbot.Bot.Commands.Utils.Changelog
{ {
public class CommitInfoDto public class CommitInfoDto
{ {
[JsonPropertyName("author")]
public CommitAuthorDto Author { get; set; } public CommitAuthorDto Author { get; set; }
[JsonPropertyName("message")]
public string Message { get; set; } public string Message { get; set; }
} }
} }

View file

@ -28,7 +28,6 @@
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0-preview.2.21154.6" /> <PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0-preview.2.21154.6" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0-preview.2.21154.6" /> <PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0-preview.2.21154.6" />
<PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0-preview.2.21154.6" /> <PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0-preview.2.21154.6" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="NLog" Version="4.7.2" /> <PackageReference Include="NLog" Version="4.7.2" />
<PackageReference Include="NLog.Config" Version="4.7.2" /> <PackageReference Include="NLog.Config" Version="4.7.2" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.0-preview2" /> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.0-preview2" />

View file

@ -5,7 +5,6 @@ using System.Text;
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using System.Threading.Tasks; using System.Threading.Tasks;
using Newtonsoft.Json;
namespace Geekbot.Core namespace Geekbot.Core
{ {
@ -39,7 +38,7 @@ namespace Geekbot.Core
httpClient.Dispose(); httpClient.Dispose();
} }
return JsonConvert.DeserializeObject<TResponse>(stringResponse); return JsonSerializer.Deserialize<TResponse>(stringResponse);
} }
public static async Task<TResponse> Post<TResponse>(Uri location, object data, HttpClient httpClient = null, bool disposeClient = true) public static async Task<TResponse> Post<TResponse>(Uri location, object data, HttpClient httpClient = null, bool disposeClient = true)
@ -48,7 +47,7 @@ namespace Geekbot.Core
httpClient.BaseAddress = location; httpClient.BaseAddress = location;
var content = new StringContent( var content = new StringContent(
System.Text.Json.JsonSerializer.Serialize(data, new JsonSerializerOptions() { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }), JsonSerializer.Serialize(data, new JsonSerializerOptions() { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }),
Encoding.UTF8, Encoding.UTF8,
"application/json" "application/json"
); );
@ -61,7 +60,7 @@ namespace Geekbot.Core
httpClient.Dispose(); httpClient.Dispose();
} }
return JsonConvert.DeserializeObject<TResponse>(stringResponse); return JsonSerializer.Deserialize<TResponse>(stringResponse);
} }
public static async Task Post(Uri location, object data, HttpClient httpClient = null, bool disposeClient = true) public static async Task Post(Uri location, object data, HttpClient httpClient = null, bool disposeClient = true)