From 68309438cf30679d6581d6cfbfeea0c028d9ed04 Mon Sep 17 00:00:00 2001 From: Prosperoh <18399228+Prosperoh@users.noreply.github.com> Date: Wed, 14 Sep 2022 13:04:12 +0200 Subject: [PATCH] Fix as_date()'s std::bad_cast with decimal value (#240) Signed-off-by: Dominik Thalhammer Co-authored-by: Prosperoh Co-authored-by: Dominik Thalhammer --- include/jwt-cpp/jwt.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/include/jwt-cpp/jwt.h b/include/jwt-cpp/jwt.h index a31d913..428163b 100644 --- a/include/jwt-cpp/jwt.h +++ b/include/jwt-cpp/jwt.h @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -2253,11 +2254,18 @@ namespace jwt { typename json_traits::string_type as_string() const { return json_traits::as_string(val); } /** - * Get the contained JSON value as a date + * \brief Get the contained JSON value as a date + * + * If the value is a decimal, it is rounded up to the closest integer + * * \return content as date * \throw std::bad_cast Content was not a date */ - date as_date() const { return std::chrono::system_clock::from_time_t(as_int()); } + date as_date() const { + using std::chrono::system_clock; + if (get_type() == json::type::number) return system_clock::from_time_t(std::round(as_number())); + return system_clock::from_time_t(as_int()); + } /** * Get the contained JSON value as an array