using System.Text.Json; using Microsoft.AspNetCore.Http; namespace BoardApp.Session; public static class SessionExtensions { private static readonly JsonSerializerOptions JsonOpts = new() { PropertyNameCaseInsensitive = true }; public static void SetJson(this ISession session, string key, T value) { session.SetString(key, JsonSerializer.Serialize(value, JsonOpts)); } public static T? GetJson(this ISession session, string key) { var raw = session.GetString(key); if (string.IsNullOrWhiteSpace(raw)) return default; try { return JsonSerializer.Deserialize(raw, JsonOpts); } catch { return default; } } }