From 93f32a18a119aac34134b627ca23f25c056f3e93 Mon Sep 17 00:00:00 2001 From: black-sliver <59490463+black-sliver@users.noreply.github.com> Date: Sat, 5 Mar 2022 01:26:16 +0100 Subject: [PATCH] allow ints as strings in json --- src/core/jsonutil.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/core/jsonutil.h b/src/core/jsonutil.h index 9f2e07fc..da9f5a36 100644 --- a/src/core/jsonutil.h +++ b/src/core/jsonutil.h @@ -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(); + if (j.type() == nlohmann::json::value_t::number_unsigned) return (int)j.get(); // TODO: float? - return (j.type() == nlohmann::json::value_t::number_integer) ? j.get() : - (j.type() == nlohmann::json::value_t::number_unsigned) ? (int)j.get() : dflt; + if (j.is_string()) { + auto s = j.get(); + 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) {