Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions include/jwt-cpp/jwt.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <algorithm>
#include <chrono>
#include <cmath>
#include <codecvt>
#include <functional>
#include <iterator>
Expand Down Expand Up @@ -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
Expand Down