namespace Kit.Helpers
{
    using Microsoft.AspNetCore.Http;
    public static class MicrosoftAspNetCoreHttpSessionExtentions
    {
        public static T GetValue<T>(this ISession session, string key)
        {
            string json = session.GetString(key);
            return json.JsonDeserialize<T>();
        }

        public static T GetValueOrDefault<T>(this ISession session, string key)
        {
            string json = session.GetString(key);
            T result;
            try
            {
                result = json.JsonDeserialize<T>();
            }
            catch
            {
                return default;
            }
            return result;
        }

        public static void SetValue(this ISession session, string key, object obj)
        {
            session.SetString(key, obj.JsonSerialize());
        }
    }
}