Fix as_date()'s std::bad_cast with decimal value (#240)

Signed-off-by: Dominik Thalhammer <dominik@thalhammer.it>
Co-authored-by: Prosperoh <prosperoh@protonmail.com>
Co-authored-by: Dominik Thalhammer <dominik@thalhammer.it>
This commit is contained in:
Prosperoh
2022-09-14 13:04:12 +02:00
committed by GitHub
parent 3ed4ff9704
commit 68309438cf

View File

@@ -23,6 +23,7 @@
#include <algorithm>
#include <chrono>
#include <cmath>
#include <codecvt>
#include <functional>
#include <iterator>
@@ -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