From cf0cd743b811ee0f65ef43ae8b61108e80e9de6e Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Mon, 1 Nov 2021 01:04:20 +0100 Subject: [PATCH] Get rid of Newtonsoft.Json everywhere but the HTTP abstractions --- src/Bot/Bot.csproj | 1 - .../Integrations/LolMmr/LolMrrInfoDto.cs | 5 ++- .../Corona/CoronaApiCountryResponseDto.cs | 10 ++--- src/Core/WikipediaClient/Page/PageApiUrls.cs | 12 +++++ .../Page/PageContentUrlCollection.cs | 7 ++- .../WikipediaClient/Page/PageContentUrls.cs | 8 ++++ .../WikipediaClient/Page/PageCoordinates.cs | 7 ++- src/Core/WikipediaClient/Page/PageImage.cs | 6 +++ .../WikipediaClient/Page/PageNamespace.cs | 7 ++- src/Core/WikipediaClient/Page/PagePreview.cs | 44 +++++++++---------- src/Core/WikipediaClient/Page/PageTitles.cs | 12 +++-- src/Core/WikipediaClient/Page/PageTypes.cs | 2 + src/Core/WikipediaClient/WikipediaClient.cs | 4 +- .../Callback/CallbackController.cs | 7 ++- .../Callback/CallbackTokenResponseDto.cs | 21 ++++++--- 15 files changed, 105 insertions(+), 48 deletions(-) diff --git a/src/Bot/Bot.csproj b/src/Bot/Bot.csproj index 30f56d8..7bf1ced 100644 --- a/src/Bot/Bot.csproj +++ b/src/Bot/Bot.csproj @@ -27,7 +27,6 @@ - diff --git a/src/Bot/Commands/Integrations/LolMmr/LolMrrInfoDto.cs b/src/Bot/Commands/Integrations/LolMmr/LolMrrInfoDto.cs index 18b096a..dfde27b 100644 --- a/src/Bot/Commands/Integrations/LolMmr/LolMrrInfoDto.cs +++ b/src/Bot/Commands/Integrations/LolMmr/LolMrrInfoDto.cs @@ -1,10 +1,11 @@ -using Newtonsoft.Json; +using System.Text.Json.Serialization; namespace Geekbot.Bot.Commands.Integrations.LolMmr { public class LolMrrInfoDto { - [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] + [JsonPropertyName("avg")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public decimal Avg { get; set; } = 0; } } \ No newline at end of file diff --git a/src/Bot/Commands/Utils/Corona/CoronaApiCountryResponseDto.cs b/src/Bot/Commands/Utils/Corona/CoronaApiCountryResponseDto.cs index 84da7f0..2a773bb 100644 --- a/src/Bot/Commands/Utils/Corona/CoronaApiCountryResponseDto.cs +++ b/src/Bot/Commands/Utils/Corona/CoronaApiCountryResponseDto.cs @@ -1,19 +1,19 @@ -using Newtonsoft.Json; +using System.Text.Json.Serialization; namespace Geekbot.Bot.Commands.Utils.Corona { public record CoronaApiCountryResponseDto { - [JsonProperty("country")] + [JsonPropertyName("country")] public string Country { get; init; } - [JsonProperty("cases")] + [JsonPropertyName("cases")] public decimal Cases { get; init; } - [JsonProperty("deaths")] + [JsonPropertyName("deaths")] public decimal Deaths { get; init; } - [JsonProperty("recovered")] + [JsonPropertyName("recovered")] public decimal Recovered { get; init; } } } \ No newline at end of file diff --git a/src/Core/WikipediaClient/Page/PageApiUrls.cs b/src/Core/WikipediaClient/Page/PageApiUrls.cs index 6e3a1b6..59d3258 100644 --- a/src/Core/WikipediaClient/Page/PageApiUrls.cs +++ b/src/Core/WikipediaClient/Page/PageApiUrls.cs @@ -1,14 +1,26 @@ using System; +using System.Text.Json.Serialization; namespace Geekbot.Core.WikipediaClient.Page { public class PageApiUrls { + [JsonPropertyName("summary")] public Uri Summary { get; set; } + + [JsonPropertyName("metadata")] public Uri Metadata { get; set; } + + [JsonPropertyName("references")] public Uri References { get; set; } + + [JsonPropertyName("media")] public Uri Media { get; set; } + + [JsonPropertyName("edit_html")] public Uri EditHtml { get; set; } + + [JsonPropertyName("talk_page_html")] public Uri TalkPageHtml { get; set; } } } \ No newline at end of file diff --git a/src/Core/WikipediaClient/Page/PageContentUrlCollection.cs b/src/Core/WikipediaClient/Page/PageContentUrlCollection.cs index 3b567a7..39bbc0c 100644 --- a/src/Core/WikipediaClient/Page/PageContentUrlCollection.cs +++ b/src/Core/WikipediaClient/Page/PageContentUrlCollection.cs @@ -1,8 +1,13 @@ -namespace Geekbot.Core.WikipediaClient.Page +using System.Text.Json.Serialization; + +namespace Geekbot.Core.WikipediaClient.Page { public class PageContentUrlCollection { + [JsonPropertyName("desktop")] public PageContentUrls Desktop { get; set; } + + [JsonPropertyName("mobile")] public PageContentUrls Mobile { get; set; } } } \ No newline at end of file diff --git a/src/Core/WikipediaClient/Page/PageContentUrls.cs b/src/Core/WikipediaClient/Page/PageContentUrls.cs index ca30b43..8da17c4 100644 --- a/src/Core/WikipediaClient/Page/PageContentUrls.cs +++ b/src/Core/WikipediaClient/Page/PageContentUrls.cs @@ -1,12 +1,20 @@ using System; +using System.Text.Json.Serialization; namespace Geekbot.Core.WikipediaClient.Page { public class PageContentUrls { + [JsonPropertyName("page")] public Uri Page { get; set; } + + [JsonPropertyName("revisions")] public Uri Revisions { get; set; } + + [JsonPropertyName("edit")] public Uri Edit { get; set; } + + [JsonPropertyName("talk")] public Uri Talk { get; set; } } } \ No newline at end of file diff --git a/src/Core/WikipediaClient/Page/PageCoordinates.cs b/src/Core/WikipediaClient/Page/PageCoordinates.cs index 52c31a4..8537325 100644 --- a/src/Core/WikipediaClient/Page/PageCoordinates.cs +++ b/src/Core/WikipediaClient/Page/PageCoordinates.cs @@ -1,8 +1,13 @@ -namespace Geekbot.Core.WikipediaClient.Page +using System.Text.Json.Serialization; + +namespace Geekbot.Core.WikipediaClient.Page { public class PageCoordinates { + [JsonPropertyName("lat")] public float Lat { get; set; } + + [JsonPropertyName("lon")] public float Lon { get; set; } } } \ No newline at end of file diff --git a/src/Core/WikipediaClient/Page/PageImage.cs b/src/Core/WikipediaClient/Page/PageImage.cs index 8c4fb82..0d0429a 100644 --- a/src/Core/WikipediaClient/Page/PageImage.cs +++ b/src/Core/WikipediaClient/Page/PageImage.cs @@ -1,11 +1,17 @@ using System; +using System.Text.Json.Serialization; namespace Geekbot.Core.WikipediaClient.Page { public class PageImage { + [JsonPropertyName("source")] public Uri Source { get; set; } + + [JsonPropertyName("width")] public int Width { get; set; } + + [JsonPropertyName("height")] public int Height { get; set; } } diff --git a/src/Core/WikipediaClient/Page/PageNamespace.cs b/src/Core/WikipediaClient/Page/PageNamespace.cs index 0691ac3..29eaba8 100644 --- a/src/Core/WikipediaClient/Page/PageNamespace.cs +++ b/src/Core/WikipediaClient/Page/PageNamespace.cs @@ -1,8 +1,13 @@ -namespace Geekbot.Core.WikipediaClient.Page +using System.Text.Json.Serialization; + +namespace Geekbot.Core.WikipediaClient.Page { public class PageNamespace { + [JsonPropertyName("id")] public ulong Id { get; set; } + + [JsonPropertyName("text")] public string Text { get; set; } } } \ No newline at end of file diff --git a/src/Core/WikipediaClient/Page/PagePreview.cs b/src/Core/WikipediaClient/Page/PagePreview.cs index 1c1749d..b87a05e 100644 --- a/src/Core/WikipediaClient/Page/PagePreview.cs +++ b/src/Core/WikipediaClient/Page/PagePreview.cs @@ -1,67 +1,65 @@ using System; -using Newtonsoft.Json; -using Newtonsoft.Json.Converters; +using System.Text.Json.Serialization; namespace Geekbot.Core.WikipediaClient.Page { public class PagePreview { - [JsonProperty("type")] - [JsonConverter(typeof(StringEnumConverter))] + [JsonPropertyName("type")] public PageTypes Type { get; set; } - [JsonProperty("title")] + [JsonPropertyName("title")] public string Title { get; set; } - [JsonProperty("displaytitle")] + [JsonPropertyName("displaytitle")] public string Displaytitle { get; set; } - [JsonProperty("namespace")] + [JsonPropertyName("namespace")] public PageNamespace Namespace { get; set; } - [JsonProperty("titles")] + [JsonPropertyName("titles")] public PageTitles Titles { get; set; } - [JsonProperty("pageid")] + [JsonPropertyName("pageid")] public ulong Pageid { get; set; } - [JsonProperty("thumbnail")] + [JsonPropertyName("thumbnail")] public PageImage Thumbnail { get; set; } - [JsonProperty("originalimage")] + [JsonPropertyName("originalimage")] public PageImage Originalimage { get; set; } - [JsonProperty("lang")] + [JsonPropertyName("lang")] public string Lang { get; set; } - [JsonProperty("dir")] + [JsonPropertyName("dir")] public string Dir { get; set; } - [JsonProperty("revision")] - public ulong Revision { get; set; } + [JsonPropertyName("revision")] + public string Revision { get; set; } - [JsonProperty("tid")] + [JsonPropertyName("tid")] public string Tid { get; set; } - [JsonProperty("timestamp")] + [JsonPropertyName("timestamp")] public DateTimeOffset Timestamp { get; set; } - [JsonProperty("description")] + [JsonPropertyName("description")] public string Description { get; set; } - [JsonProperty("coordinates")] + [JsonPropertyName("coordinates")] public PageCoordinates Coordinates { get; set; } - [JsonProperty("content_urls")] + [JsonPropertyName("content_urls")] public PageContentUrlCollection ContentUrls { get; set; } - [JsonProperty("api_urls")] + [JsonPropertyName("api_urls")] public PageApiUrls ApiUrls { get; set; } - [JsonProperty("extract")] + [JsonPropertyName("extract")] public string Extract { get; set; } - [JsonProperty("extract_html")] + [JsonPropertyName("extract_html")] public string ExtractHtml { get; set; } } } \ No newline at end of file diff --git a/src/Core/WikipediaClient/Page/PageTitles.cs b/src/Core/WikipediaClient/Page/PageTitles.cs index ad83b27..f550c38 100644 --- a/src/Core/WikipediaClient/Page/PageTitles.cs +++ b/src/Core/WikipediaClient/Page/PageTitles.cs @@ -1,10 +1,16 @@ -namespace Geekbot.Core.WikipediaClient.Page +using System.Text.Json.Serialization; + +namespace Geekbot.Core.WikipediaClient.Page { public class PageTitles { + [JsonPropertyName("Canonical")] public string Canonical { get; set; } - public string Normalized { get; set; } - public string Display { get; set; } + [JsonPropertyName("Normalized")] + public string Normalized { get; set; } + + [JsonPropertyName("Display")] + public string Display { get; set; } } } \ No newline at end of file diff --git a/src/Core/WikipediaClient/Page/PageTypes.cs b/src/Core/WikipediaClient/Page/PageTypes.cs index 8bc9f64..9ad748a 100644 --- a/src/Core/WikipediaClient/Page/PageTypes.cs +++ b/src/Core/WikipediaClient/Page/PageTypes.cs @@ -1,7 +1,9 @@ using System.Runtime.Serialization; +using System.Text.Json.Serialization; namespace Geekbot.Core.WikipediaClient.Page { + [JsonConverter(typeof(JsonStringEnumConverter))] public enum PageTypes { [EnumMember(Value = "standard")] diff --git a/src/Core/WikipediaClient/WikipediaClient.cs b/src/Core/WikipediaClient/WikipediaClient.cs index f577d92..cf13277 100644 --- a/src/Core/WikipediaClient/WikipediaClient.cs +++ b/src/Core/WikipediaClient/WikipediaClient.cs @@ -1,7 +1,7 @@ using System.Net.Http; +using System.Text.Json; using System.Threading.Tasks; using Geekbot.Core.WikipediaClient.Page; -using Newtonsoft.Json; namespace Geekbot.Core.WikipediaClient { @@ -19,7 +19,7 @@ namespace Geekbot.Core.WikipediaClient response.EnsureSuccessStatusCode(); var stringResponse = await response.Content.ReadAsStringAsync(); - return JsonConvert.DeserializeObject(stringResponse); + return JsonSerializer.Deserialize(stringResponse); } } } \ No newline at end of file diff --git a/src/Web/Controllers/Callback/CallbackController.cs b/src/Web/Controllers/Callback/CallbackController.cs index 156cad9..91cb6ad 100644 --- a/src/Web/Controllers/Callback/CallbackController.cs +++ b/src/Web/Controllers/Callback/CallbackController.cs @@ -2,11 +2,10 @@ using System; using System.Collections.Generic; using System.Net.Http; using System.Net.Http.Headers; +using System.Text.Json; using System.Threading.Tasks; -using Discord.WebSocket; using Geekbot.Core.GlobalSettings; using Microsoft.AspNetCore.Mvc; -using Newtonsoft.Json; namespace Geekbot.Web.Controllers.Callback { @@ -45,8 +44,8 @@ namespace Geekbot.Web.Controllers.Callback result.EnsureSuccessStatusCode(); var stringResponse = await result.Content.ReadAsStringAsync(); - var responseData = JsonConvert.DeserializeObject(stringResponse); - token = responseData.access_token; + var responseData = JsonSerializer.Deserialize(stringResponse); + token = responseData.AccessToken; } return new RedirectResult($"https://geekbot.pizzaandcoffee.rocks/login?token={token}", false); diff --git a/src/Web/Controllers/Callback/CallbackTokenResponseDto.cs b/src/Web/Controllers/Callback/CallbackTokenResponseDto.cs index 37d2c32..3292300 100644 --- a/src/Web/Controllers/Callback/CallbackTokenResponseDto.cs +++ b/src/Web/Controllers/Callback/CallbackTokenResponseDto.cs @@ -1,11 +1,22 @@ +using System.Text.Json.Serialization; + namespace Geekbot.Web.Controllers.Callback { public class CallbackTokenResponseDto { - public string access_token { get; set; } - public string token_type { get; set; } - public int expires_in { get; set; } - public string refresh_token { get; set; } - public string scope { get; set; } + [JsonPropertyName("access_token")] + public string AccessToken { get; set; } + + [JsonPropertyName("token_type")] + public string TokenType { get; set; } + + [JsonPropertyName("expires_in")] + public int ExpiresIn { get; set; } + + [JsonPropertyName("refresh_token")] + public string RefreshToken { get; set; } + + [JsonPropertyName("scope")] + public string Scope { get; set; } } } \ No newline at end of file