Fixing JSON
This commit is contained in:
@@ -1,20 +1,29 @@
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Server.Networking.Messages;
|
||||
|
||||
public sealed class JsonSerializerAdapter : IMessageSerializer
|
||||
namespace Server.Networking.Messages
|
||||
{
|
||||
private static readonly JsonSerializerOptions Options = new()
|
||||
public sealed class JsonSerializerAdapter : IMessageSerializer
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
WriteIndented = false,
|
||||
IncludeFields = true
|
||||
};
|
||||
private static readonly JsonSerializerOptions Options = CreateOptions();
|
||||
|
||||
public byte[] Serialize<T>(T message) =>
|
||||
JsonSerializer.SerializeToUtf8Bytes(message, Options);
|
||||
private static JsonSerializerOptions CreateOptions()
|
||||
{
|
||||
var o = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
PropertyNameCaseInsensitive = true, // accept opCode / OpCode
|
||||
WriteIndented = false,
|
||||
IncludeFields = true
|
||||
};
|
||||
o.Converters.Add(new FlexibleByteArrayConverter());
|
||||
return o;
|
||||
}
|
||||
|
||||
public T Deserialize<T>(ReadOnlyMemory<byte> bytes) =>
|
||||
JsonSerializer.Deserialize<T>(bytes.Span, Options)!;
|
||||
public byte[] Serialize<T>(T message) =>
|
||||
JsonSerializer.SerializeToUtf8Bytes(message, Options);
|
||||
|
||||
public T Deserialize<T>(ReadOnlyMemory<byte> bytes) =>
|
||||
JsonSerializer.Deserialize<T>(bytes.Span, Options)!;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user