Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

naive wave 1 of moving to STJ #1026

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

<PackageReference Update="AspNet.Security.OAuth.Discord" Version="8.0.0" />

<PackageReference Update="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.3" />
<PackageReference Update="Microsoft.AspNetCore.SpaServices.Extensions" Version="8.0.3" />
<PackageReference Update="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
<PackageReference Update="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
Expand All @@ -39,8 +38,6 @@
<PackageReference Update="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
<PackageReference Update="Microsoft.NET.Test.Sdk" Version="17.*" />

<PackageReference Update="Newtonsoft.Json" Version="13.0.3" />

<PackageReference Update="LZStringCSharp" Version="1.4.0" />

<PackageReference Update="LinqKit.Microsoft.EntityFrameworkCore" Version="8.1.5" />
Expand Down
4 changes: 2 additions & 2 deletions src/Modix.Analyzers/Modix.Analyzers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.10.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.10.0" />
</ItemGroup>

</Project>
1 change: 0 additions & 1 deletion src/Modix.Bot/Modix.Bot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<PackageReference Include="Humanizer.Core" />
<PackageReference Include="Microsoft.Extensions.Hosting" />
<PackageReference Include="Microsoft.Extensions.Http" />
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="LZStringCSharp" />
</ItemGroup>
<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/Modix.Bot/Modules/ReplModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Discord;
Expand All @@ -12,7 +13,6 @@
using Modix.Services.CodePaste;
using Modix.Services.CommandHelp;
using Modix.Services.Utilities;
using Newtonsoft.Json;

namespace Modix.Bot.Modules
{
Expand Down Expand Up @@ -106,7 +106,7 @@ await ModifyOrSendErrorEmbed("Received an invalid response from the REPL service
return;
}

var parsedResult = JsonConvert.DeserializeObject<Result>(await res.Content.ReadAsStringAsync());
var parsedResult = JsonSerializer.Deserialize<Result>(await res.Content.ReadAsStringAsync());

var embed = await BuildEmbedAsync(guildUser, parsedResult);

Expand Down
14 changes: 11 additions & 3 deletions src/Modix.Services/CodePaste/CodePasteService.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using System.Net.Http;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Discord;
using Modix.Data.Utilities;
using Modix.Services.Utilities;
using Newtonsoft.Json.Linq;

namespace Modix.Services.CodePaste
{
Expand Down Expand Up @@ -46,12 +47,19 @@ public async Task<string> UploadCodeAsync(string code)
throw new Exception($"{response.StatusCode} returned when calling {response.RequestMessage?.RequestUri}. Response body: {body}");
}

var urlResponse = await response.Content.ReadAsStringAsync();
var pasteKey = JObject.Parse(urlResponse)["key"]?.Value<string>();
var responseStream = await response.Content.ReadAsStreamAsync();
var uploadCodeResponse = await JsonSerializer.DeserializeAsync<UploadCodeResponse>(responseStream);
var pasteKey = uploadCodeResponse.Key;

return $"{ApiReferenceUrl}{pasteKey}";
}

public class UploadCodeResponse
{
[JsonPropertyName("key")]
public string Key { get; set; }
}

/// <summary>
/// Uploads the code in the given message to the service, and returns the URL to the post.
/// </summary>
Expand Down
1 change: 0 additions & 1 deletion src/Modix.Services/Modix.Services.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<PackageReference Include="Humanizer.Core" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" />
<PackageReference Include="Microsoft.Extensions.Http" />
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="Serilog" />
<PackageReference Include="SixLabors.ImageSharp" />
</ItemGroup>
Expand Down
57 changes: 27 additions & 30 deletions src/Modix.Services/StackExchange/StackExchangeResponse.cs
Original file line number Diff line number Diff line change
@@ -1,92 +1,89 @@
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Modix.Services.StackExchange
{
[JsonObject]
public class StackExchangeResponse
{
[JsonProperty("has_more")]
[JsonPropertyName("has_more")]
public bool HasMore { get; set; }

[JsonProperty("quota_max")]
[JsonPropertyName("quota_max")]
public int QuotaMax { get; set; }

[JsonProperty("quota_remaining")]
[JsonPropertyName("quota_remaining")]
public int QuotaRemaining { get; set; }

[JsonProperty("items")]
[JsonPropertyName("items")]
public ICollection<StackExchangeResponseItem> Items { get; set; }
}

[JsonObject]
public class StackExchangeResponseItem
{
[JsonProperty("tags")]
[JsonPropertyName("tags")]
public ICollection<string> Tags { get; set; }

[JsonProperty("owner")]
[JsonPropertyName("owner")]
public StackExchangeResponseOwner Owner { get; set; }

[JsonProperty("is_answered")]
[JsonPropertyName("is_answered")]
public bool IsAnswered { get; set; }

[JsonProperty("view_count")]
[JsonPropertyName("view_count")]
public int ViewCount { get; set; }

[JsonProperty("protected_date")]
[JsonPropertyName("protected_date")]
public int ProtectedDate { get; set; }

[JsonProperty("accepted_answer_id")]
[JsonPropertyName("accepted_answer_id")]
public int AcceptedAnswerId { get; set; }

[JsonProperty("answer_count")]
[JsonPropertyName("answer_count")]
public int AnswerCount { get; set; }

[JsonProperty("score")]
[JsonPropertyName("score")]
public int Score { get; set; }

[JsonProperty("last_activity_date")]
[JsonPropertyName("last_activity_date")]
public long LastActivityDate { get; set; }

[JsonProperty("creation_date")]
[JsonPropertyName("creation_date")]
public long CreationDate { get; set; }

[JsonProperty("last_edit_date")]
[JsonPropertyName("last_edit_date")]
public long LastEditDate { get; set; }

[JsonProperty("question_id")]
[JsonPropertyName("question_id")]
public int QuestionId { get; set; }

[JsonProperty("link")]
[JsonPropertyName("link")]
public string Link { get; set; }

[JsonProperty("title")]
[JsonPropertyName("title")]
public string Title { get; set; }
}

[JsonObject]
public class StackExchangeResponseOwner
{
[JsonProperty("reputation")]
[JsonPropertyName("reputation")]
public int Reputation { get; set; }

[JsonProperty("user_id")]
[JsonPropertyName("user_id")]
public int UserId { get; set; }

[JsonProperty("user_type")]
[JsonPropertyName("user_type")]
public string UserType { get; set; }

[JsonProperty("accept_rate")]
[JsonPropertyName("accept_rate")]
public int AcceptRate { get; set; }

[JsonProperty("profile_image")]
[JsonPropertyName("profile_image")]
public string ProfileImage { get; set; }

[JsonProperty("display_name")]
[JsonPropertyName("display_name")]
public string DisplayName { get; set; }

[JsonProperty("link")]
[JsonPropertyName("link")]
public string Link { get; set; }
}
}
4 changes: 2 additions & 2 deletions src/Modix.Services/StackExchange/StackExchangeService.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Modix.Services.Utilities;
using Newtonsoft.Json;
using System;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;

namespace Modix.Services.StackExchange
Expand Down Expand Up @@ -39,7 +39,7 @@ public async Task<StackExchangeResponse> GetStackExchangeResultsAsync(string tok
}

var jsonResponse = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<StackExchangeResponse>(jsonResponse);
return JsonSerializer.Deserialize<StackExchangeResponse>(jsonResponse);
}

protected IHttpClientFactory HttpClientFactory { get; }
Expand Down
19 changes: 12 additions & 7 deletions src/Modix.Services/Utilities/DiscordWebhookSink.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System;
using System.Collections.Concurrent;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using System.Threading;
using System.Threading.Tasks;
using Discord;
using Discord.Webhook;
using Modix.Services.CodePaste;
using Newtonsoft.Json;
using Serilog;
using Serilog.Configuration;
using Serilog.Core;
Expand All @@ -18,7 +20,7 @@ public sealed class DiscordWebhookSink : ILogEventSink, IAsyncDisposable
private readonly Lazy<CodePasteService> _codePasteService;
private readonly DiscordWebhookClient _discordWebhookClient;
private readonly IFormatProvider _formatProvider;
private readonly JsonSerializerSettings _jsonSerializerSettings;
private readonly JsonSerializerOptions _jsonSerializerOptions;
private readonly CancellationTokenSource _cancellationTokenSource;
private readonly Task _logEventProcessorTask;
private readonly BlockingCollection<LogEvent> _logEventQueue;
Expand All @@ -33,11 +35,14 @@ public DiscordWebhookSink(
_discordWebhookClient = new DiscordWebhookClient(webhookId, webhookToken);
_formatProvider = formatProvider;

_jsonSerializerSettings = new JsonSerializerSettings
_jsonSerializerOptions = new JsonSerializerOptions
{
Formatting = Formatting.Indented,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
ContractResolver = new ExceptionContractResolver()
WriteIndented = true,
ReferenceHandler= ReferenceHandler.IgnoreCycles,
TypeInfoResolver = new DefaultJsonTypeInfoResolver
{
Modifiers = { ExceptionJsonTypeInfoResolver.Modifier }
}
};

_cancellationTokenSource = new CancellationTokenSource();
Expand Down Expand Up @@ -73,7 +78,7 @@ public async Task ProcessLogEventItemsAsync()
.WithName($"LogLevel: {logEvent.Level}")
.WithValue(Format.Code(messagePayload.TruncateTo(DiscordStringTruncateLength))));

var eventAsJson = JsonConvert.SerializeObject(logEvent, _jsonSerializerSettings);
var eventAsJson = JsonSerializer.Serialize(logEvent, _jsonSerializerOptions);

var url = await _codePasteService.Value.UploadCodeAsync(eventAsJson);

Expand Down
Loading