Remove usages of Newtonsoft.Json from StatusHost (#6082)

This commit is contained in:
Pieter-Jan Briers
2022-01-10 01:41:46 +01:00
committed by GitHub
parent 4be7c4a63e
commit 2125f1dda9
3 changed files with 27 additions and 24 deletions

View File

@@ -1,5 +1,5 @@
using System; using System;
using Newtonsoft.Json.Linq; using System.Text.Json.Nodes;
using Robust.Server.ServerStatus; using Robust.Server.ServerStatus;
using Robust.Shared.IoC; using Robust.Shared.IoC;
using Robust.Shared.ViewVariables; using Robust.Shared.ViewVariables;
@@ -24,7 +24,7 @@ namespace Content.Server.GameTicking
IoCManager.Resolve<IStatusHost>().OnStatusRequest += GetStatusResponse; IoCManager.Resolve<IStatusHost>().OnStatusRequest += GetStatusResponse;
} }
private void GetStatusResponse(JObject jObject) private void GetStatusResponse(JsonNode jObject)
{ {
// This method is raised from another thread, so this better be thread safe! // This method is raised from another thread, so this better be thread safe!
lock (_statusShellLock) lock (_statusShellLock)

View File

@@ -1,12 +1,12 @@
using System; using System;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Text; using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks; using System.Threading.Tasks;
using Content.Server.Chat.Managers; using Content.Server.Chat.Managers;
using Content.Shared;
using Content.Shared.CCVar; using Content.Shared.CCVar;
using Newtonsoft.Json;
using Robust.Server.ServerStatus; using Robust.Server.ServerStatus;
using Robust.Shared.Asynchronous; using Robust.Shared.Asynchronous;
using Robust.Shared.Configuration; using Robust.Shared.Configuration;
@@ -62,9 +62,7 @@ namespace Content.Server.MoMMI
Contents = messageObject Contents = messageObject
}; };
var jsonMessage = JsonConvert.SerializeObject(sentMessage); var request = await _httpClient.PostAsJsonAsync(url, sentMessage);
var request =
await _httpClient.PostAsync(url, new StringContent(jsonMessage, Encoding.UTF8, "application/json"));
if (!request.IsSuccessStatusCode) if (!request.IsSuccessStatusCode)
{ {
@@ -92,7 +90,7 @@ namespace Content.Server.MoMMI
{ {
message = context.RequestBodyJson<OOCPostMessage>(); message = context.RequestBodyJson<OOCPostMessage>();
} }
catch (JsonSerializationException) catch (JsonException)
{ {
// message null so enters block down below. // message null so enters block down below.
} }
@@ -116,34 +114,39 @@ namespace Content.Server.MoMMI
return true; return true;
} }
[JsonObject(MemberSerialization.Fields)] private sealed class MoMMIMessageBase
private class MoMMIMessageBase
{ {
[JsonProperty("password")] public string Password = null!; [JsonInclude] [JsonPropertyName("password")]
public string Password = null!;
[JsonProperty("type")] public string Type = null!; [JsonInclude] [JsonPropertyName("type")]
public string Type = null!;
[JsonProperty("contents")] public object Contents = null!; [JsonInclude] [JsonPropertyName("contents")]
public object Contents = null!;
} }
[JsonObject(MemberSerialization.Fields)]
private class MoMMIMessageOOC private class MoMMIMessageOOC
{ {
[JsonProperty("sender")] public string Sender = null!; [JsonInclude] [JsonPropertyName("sender")]
public string Sender = null!;
[JsonProperty("contents")] public string Contents = null!; [JsonInclude] [JsonPropertyName("contents")]
public string Contents = null!;
} }
[JsonObject(MemberSerialization.Fields, ItemRequired = Required.Always)]
private class OOCPostMessage private class OOCPostMessage
{ {
#pragma warning disable CS0649 #pragma warning disable CS0649
[JsonProperty("password")] public string Password = null!; [JsonInclude] [JsonPropertyName("password")]
public string Password = null!;
[JsonProperty("sender")] public string Sender = null!; [JsonInclude] [JsonPropertyName("sender")]
public string Sender = null!;
[JsonProperty("contents")] public string Contents = null!; [JsonInclude] [JsonPropertyName("contents")]
#pragma warning restore CS0649 public string Contents = null!;
#pragma warning restore CS0649
} }
} }
} }