From 4b5288b2372daee29c8dd8a0a43c14647872f49a Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Tue, 30 Apr 2024 10:42:46 +0200 Subject: [PATCH] use AppSerializerContext singleton --- .../TanksServer/Interactivity/AppSerializerContext.cs | 2 ++ .../TanksServer/Interactivity/PlayerInfoConnection.cs | 3 +-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/tanks-backend/TanksServer/Interactivity/AppSerializerContext.cs b/tanks-backend/TanksServer/Interactivity/AppSerializerContext.cs index 46d6ba2..004c313 100644 --- a/tanks-backend/TanksServer/Interactivity/AppSerializerContext.cs +++ b/tanks-backend/TanksServer/Interactivity/AppSerializerContext.cs @@ -1,3 +1,4 @@ +using System.Text.Json; using System.Text.Json.Serialization; namespace TanksServer.Interactivity; @@ -6,4 +7,5 @@ namespace TanksServer.Interactivity; [JsonSerializable(typeof(IEnumerable))] [JsonSerializable(typeof(IEnumerable))] [JsonSerializable(typeof(PlayerInfo))] +[JsonSourceGenerationOptions(JsonSerializerDefaults.Web)] internal sealed partial class AppSerializerContext : JsonSerializerContext; diff --git a/tanks-backend/TanksServer/Interactivity/PlayerInfoConnection.cs b/tanks-backend/TanksServer/Interactivity/PlayerInfoConnection.cs index bc4956b..64a544e 100644 --- a/tanks-backend/TanksServer/Interactivity/PlayerInfoConnection.cs +++ b/tanks-backend/TanksServer/Interactivity/PlayerInfoConnection.cs @@ -11,7 +11,6 @@ internal sealed class PlayerInfoConnection( MapEntityManager entityManager ) : WebsocketServerConnection(logger, new ByteChannelWebSocket(rawSocket, logger, 0)) { - private readonly AppSerializerContext _context = new(new JsonSerializerOptions(JsonSerializerDefaults.Web)); private bool _wantsInfoOnTick = true; private byte[] _lastMessage = []; @@ -54,7 +53,7 @@ internal sealed class PlayerInfoConnection( } var info = new PlayerInfo(player.Name, player.Scores, player.Controls.ToDisplayString(), tankInfo); - var response = JsonSerializer.SerializeToUtf8Bytes(info, _context.PlayerInfo); + var response = JsonSerializer.SerializeToUtf8Bytes(info, AppSerializerContext.Default.PlayerInfo); if (response.SequenceEqual(_lastMessage)) return null;