Fix serializer changes and ignore local .dotnet cache
This commit is contained in:
@@ -1,19 +1,20 @@
|
||||
namespace SOR.Shared.Serialization;
|
||||
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using SOR.Networking.Abstractions.Messaging;
|
||||
using SOR.Shared.Networking;
|
||||
|
||||
/// <summary>
|
||||
/// IMessageSerializer implementation using System.Text.Json.
|
||||
///
|
||||
/// Wire format — Client to Server:
|
||||
/// Wire format - Client to Server:
|
||||
/// { "type": "chat.send", "seq": 7, "payload": { "message": "Hello" } }
|
||||
///
|
||||
/// Wire format — Server to Client (reply, ref = client seq):
|
||||
/// Wire format - Server to Client (reply, ref = client seq):
|
||||
/// { "type": "chat.send.ack", "ref": 7, "payload": { "messageId": "..." } }
|
||||
///
|
||||
/// Wire format — Server to Client (unsolicited push, ref = 0):
|
||||
/// Wire format - Server to Client (unsolicited push, ref = 0):
|
||||
/// { "type": "game.state_update", "ref": 0, "payload": { ... } }
|
||||
///
|
||||
/// To swap to MessagePack:
|
||||
@@ -21,7 +22,7 @@ using SOR.Shared.Networking;
|
||||
/// 2. In SOR.Shared.ServiceRegistration: replace AddSingleton{IMessageSerializer, SystemTextJsonSerializer}
|
||||
/// with AddSingleton{IMessageSerializer, MessagePackSerializer}
|
||||
/// 3. Update client to send/receive MessagePack frames instead of JSON
|
||||
/// 4. Nothing else changes — all handlers, all services, all tests are unaffected
|
||||
/// 4. Nothing else changes - all handlers, all services, all tests are unaffected
|
||||
///
|
||||
/// Lifetime: Singleton. This class is thread-safe (no mutable state; JsonSerializerOptions
|
||||
/// is immutable after construction).
|
||||
@@ -34,14 +35,17 @@ public sealed class SystemTextJsonSerializer : IMessageSerializer
|
||||
/// </summary>
|
||||
private static readonly JsonSerializerOptions Options = new()
|
||||
{
|
||||
// camelCase property names: C# 'SessionToken' → JSON 'sessionToken'
|
||||
// camelCase property names: C# 'SessionToken' -> JSON 'sessionToken'
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
|
||||
// Case-insensitive property matching for deserialization:
|
||||
// JSON "SessionToken" and "sessionToken" both map to C# 'SessionToken'
|
||||
PropertyNameCaseInsensitive = true,
|
||||
|
||||
// Compact output — no indentation in production
|
||||
// Allow enum payloads to be sent as strings (for example "online").
|
||||
Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) },
|
||||
|
||||
// Compact output - no indentation in production
|
||||
WriteIndented = false,
|
||||
};
|
||||
|
||||
@@ -56,18 +60,18 @@ public sealed class SystemTextJsonSerializer : IMessageSerializer
|
||||
// JsonDocument provides a DOM view of JSON bytes without full deserialization.
|
||||
// It is more efficient than JsonSerializer.Deserialize when we only need
|
||||
// a few fields and want to defer inner payload deserialization.
|
||||
// JsonDocument.Parse does not have a ReadOnlySpan{byte} overload in .NET 8.
|
||||
// .ToArray() copies the span into a byte[] — minimal overhead for the parse path.
|
||||
// JsonDocument.Parse does not have a ReadOnlySpan<byte> overload in .NET 8.
|
||||
// .ToArray() copies the span into a byte[] - minimal overhead for the parse path.
|
||||
using var doc = JsonDocument.Parse(wireBytes.ToArray());
|
||||
var root = doc.RootElement;
|
||||
|
||||
// Extract envelope fields. GetString() returns null if the property is missing
|
||||
// or not a string — the ?? throw gives a clear error instead of a null ref later.
|
||||
// or not a string - the ?? throw gives a clear error instead of a null ref later.
|
||||
var type = root.GetProperty("type").GetString()
|
||||
?? throw new JsonException("Client message missing required 'type' field.");
|
||||
|
||||
// TryGetProperty returns false (not an exception) if the field is absent.
|
||||
// Seq defaults to 0 if not provided — older clients or fire-and-forget messages.
|
||||
// Seq defaults to 0 if not provided - older clients or fire-and-forget messages.
|
||||
var seq = root.TryGetProperty("seq", out var seqEl) ? seqEl.GetInt32() : 0;
|
||||
|
||||
// Re-serialize the payload sub-object to bytes for handler deserialization.
|
||||
@@ -75,7 +79,7 @@ public sealed class SystemTextJsonSerializer : IMessageSerializer
|
||||
ReadOnlyMemory<byte> payloadBytes = ReadOnlyMemory<byte>.Empty;
|
||||
if (root.TryGetProperty("payload", out var payloadEl))
|
||||
{
|
||||
// SerializeToUtf8Bytes allocates a new byte[] — this is fine since it
|
||||
// SerializeToUtf8Bytes allocates a new byte[] - this is fine since it
|
||||
// happens once per message and the bytes are owned by RawClientMessage.
|
||||
payloadBytes = JsonSerializer.SerializeToUtf8Bytes(payloadEl, Options);
|
||||
}
|
||||
@@ -105,7 +109,7 @@ public sealed class SystemTextJsonSerializer : IMessageSerializer
|
||||
/// <inheritdoc/>
|
||||
public T DeserializePayload<T>(ReadOnlyMemory<byte> payload) where T : notnull
|
||||
{
|
||||
// Span<byte> is a stack-allocated view of the memory — no copy.
|
||||
// Span<byte> is a stack-allocated view of the memory - no copy.
|
||||
return JsonSerializer.Deserialize<T>(payload.Span, Options)
|
||||
?? throw new JsonException(
|
||||
$"Payload deserialized to null for type {typeof(T).Name}. " +
|
||||
|
||||
Reference in New Issue
Block a user