Skip to content

Commit

Permalink
allow ints as strings in json
Browse files Browse the repository at this point in the history
  • Loading branch information
black-sliver committed Mar 5, 2022
1 parent 8ed6f83 commit 93f32a1
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/core/jsonutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,16 @@ static std::string to_string(const nlohmann::json& j, const std::string& key, co
}
static int to_int(const nlohmann::json& j, int dflt)
{
if (j.type() == nlohmann::json::value_t::number_integer) return j.get<int>();
if (j.type() == nlohmann::json::value_t::number_unsigned) return (int)j.get<unsigned>();
// TODO: float?
return (j.type() == nlohmann::json::value_t::number_integer) ? j.get<int>() :
(j.type() == nlohmann::json::value_t::number_unsigned) ? (int)j.get<unsigned>() : dflt;
if (j.is_string()) {
auto s = j.get<std::string>();
char* end;
long n = strtol(s.c_str(), &end, 10);
if (end && end != s.c_str() && *end == 0) return (int)n;
}
return dflt;
}
static Direction to_direction(const nlohmann::json& j, Direction dflt=Direction::UNDEFINED)
{
Expand Down

0 comments on commit 93f32a1

Please sign in to comment.