From ece3eaa3be424e249019f9e69c4cd1779cebc0eb Mon Sep 17 00:00:00 2001 From: dingo Date: Sat, 7 Mar 2026 14:15:45 -0500 Subject: [PATCH] Fix NATS serialization: use System.Text.Json for custom event types NatsUtf8PrimitivesSerializer (the default) can only handle primitives and strings, causing "Can't serialize GameStartedEvent" at runtime. Add NatsJsonSerializer implementing INatsSerialize/INatsDeserialize using System.Text.Json, and pass it explicitly to PublishAsync/SubscribeAsync in NatsEventBus so all custom NATS event payloads serialize as JSON. Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude Co-Authored-By: Happy --- .../src/SOR.Networking.Nats/NatsEventBus.cs | 10 ++++-- .../SOR.Networking.Nats/NatsJsonSerializer.cs | 34 +++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 Server/src/SOR.Networking.Nats/NatsJsonSerializer.cs diff --git a/Server/src/SOR.Networking.Nats/NatsEventBus.cs b/Server/src/SOR.Networking.Nats/NatsEventBus.cs index fe8eb72..0e689c1 100644 --- a/Server/src/SOR.Networking.Nats/NatsEventBus.cs +++ b/Server/src/SOR.Networking.Nats/NatsEventBus.cs @@ -48,7 +48,9 @@ public sealed class NatsEventBus : IEventBus { _logger.LogDebug("Publishing event to NATS subject {Subject}", subject); - await _nats.PublishAsync(subject, payload, cancellationToken: cancellationToken); + await _nats.PublishAsync(subject, payload, + serializer: NatsJsonSerializer.Default, + cancellationToken: cancellationToken); } /// @@ -88,7 +90,11 @@ public sealed class NatsEventBus : IEventBus // SubscribeAsync returns IAsyncEnumerable{NatsMsg{T}}. // NatsMsg{T} is the NATS envelope containing the deserialized data plus metadata. await foreach (var msg in _nats - .SubscribeAsync(subject, cancellationToken: cancellationToken) + .SubscribeAsync(subject, + queueGroup: null, + serializer: NatsJsonSerializer.Default, + opts: default, + cancellationToken: cancellationToken) .WithCancellation(cancellationToken)) { // Skip null data (can occur for empty NATS messages or deserialization failures). diff --git a/Server/src/SOR.Networking.Nats/NatsJsonSerializer.cs b/Server/src/SOR.Networking.Nats/NatsJsonSerializer.cs new file mode 100644 index 0000000..445d65e --- /dev/null +++ b/Server/src/SOR.Networking.Nats/NatsJsonSerializer.cs @@ -0,0 +1,34 @@ +namespace SOR.Networking.Nats; + +using System.Buffers; +using System.Text.Json; +using NATS.Client.Core; + +/// +/// System.Text.Json serializer/deserializer for NATS messages. +/// +/// NATS.Client.Core 2.3.0 does not ship a ready-made JSON registry. +/// This class implements both and +/// so it can be passed to +/// PublishAsync(serializer:) and SubscribeAsync(deserializer:). +/// +internal sealed class NatsJsonSerializer : INatsSerialize, INatsDeserialize +{ + public static readonly NatsJsonSerializer Default = new(); + + private static readonly JsonSerializerOptions Options = + new(JsonSerializerDefaults.Web); + + public void Serialize(IBufferWriter bufferWriter, T value) + { + var writer = new Utf8JsonWriter(bufferWriter); + JsonSerializer.Serialize(writer, value, Options); + writer.Flush(); + } + + public T? Deserialize(in ReadOnlySequence buffer) + { + var reader = new Utf8JsonReader(buffer); + return JsonSerializer.Deserialize(ref reader, Options); + } +}