use AppSerializerContext singleton

This commit is contained in:
Vinzenz Schroeter 2024-04-30 10:42:46 +02:00 committed by RobbersDaughter
parent a2ae6d273e
commit 4b5288b237
2 changed files with 3 additions and 2 deletions

View file

@ -1,3 +1,4 @@
using System.Text.Json;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
namespace TanksServer.Interactivity; namespace TanksServer.Interactivity;
@ -6,4 +7,5 @@ namespace TanksServer.Interactivity;
[JsonSerializable(typeof(IEnumerable<Player>))] [JsonSerializable(typeof(IEnumerable<Player>))]
[JsonSerializable(typeof(IEnumerable<string>))] [JsonSerializable(typeof(IEnumerable<string>))]
[JsonSerializable(typeof(PlayerInfo))] [JsonSerializable(typeof(PlayerInfo))]
[JsonSourceGenerationOptions(JsonSerializerDefaults.Web)]
internal sealed partial class AppSerializerContext : JsonSerializerContext; internal sealed partial class AppSerializerContext : JsonSerializerContext;

View file

@ -11,7 +11,6 @@ internal sealed class PlayerInfoConnection(
MapEntityManager entityManager MapEntityManager entityManager
) : WebsocketServerConnection(logger, new ByteChannelWebSocket(rawSocket, logger, 0)) ) : WebsocketServerConnection(logger, new ByteChannelWebSocket(rawSocket, logger, 0))
{ {
private readonly AppSerializerContext _context = new(new JsonSerializerOptions(JsonSerializerDefaults.Web));
private bool _wantsInfoOnTick = true; private bool _wantsInfoOnTick = true;
private byte[] _lastMessage = []; private byte[] _lastMessage = [];
@ -54,7 +53,7 @@ internal sealed class PlayerInfoConnection(
} }
var info = new PlayerInfo(player.Name, player.Scores, player.Controls.ToDisplayString(), tankInfo); 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)) if (response.SequenceEqual(_lastMessage))
return null; return null;