using System.Text;
using System.Text.Json;

namespace TakeoutSaaS.Module.Messaging.Serialization;

/// <summary>
/// 消息 JSON 序列化器。
/// </summary>
public sealed class JsonMessageSerializer
{
    private static readonly JsonSerializerOptions DefaultOptions = new(JsonSerializerDefaults.Web);

    /// <summary>
    /// 序列化消息。
    /// </summary>
    public byte[] Serialize<T>(T message) => Encoding.UTF8.GetBytes(JsonSerializer.Serialize(message, DefaultOptions));

    /// <summary>
    /// 反序列化消息。
    /// </summary>
    public T? Deserialize<T>(byte[] body) => JsonSerializer.Deserialize<T>(body, DefaultOptions);
}
