using System.Text.Json; using System.Text.Json.Serialization; namespace LumaTunnel.Shared.Protocol; public sealed record TunnelOpenRequest(string Host, int Port); public sealed record TunnelOpenError(TunnelErrorCode Code, string Message); public static class TunnelMessageSerializer { public static JsonSerializerOptions Options { get; } = new(JsonSerializerDefaults.Web) { PropertyNameCaseInsensitive = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }; public static byte[] Serialize(T value) => JsonSerializer.SerializeToUtf8Bytes(value, Options); public static T Deserialize(ReadOnlyMemory value) where T : class => JsonSerializer.Deserialize(value.Span, Options) ?? throw new TunnelProtocolException(TunnelErrorCode.MalformedFrame, $"Unable to deserialize {typeof(T).Name}."); }