diff --git a/src/json.hpp b/src/json.hpp index 630624a6..9fd0d05b 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -498,22 +498,23 @@ template = 0> void get_arithmetic_value(const Json& j, ArithmeticType& val) { - // unsigned must be checked first, since is_number_integer() == true for unsigned - if (j.is_number_unsigned()) + switch (static_cast(j)) { - val = static_cast(*j.template get_ptr()); - } - else if (j.is_number_integer()) - { - val = static_cast(*j.template get_ptr()); - } - else if (j.is_number_float()) - { - val = static_cast(*j.template get_ptr()); - } - else - { - JSON_THROW(std::domain_error("type must be number, but is " + type_name(j))); + case value_t::number_unsigned: + val = static_cast( + *j.template get_ptr()); + break; + case value_t::number_integer: + val = static_cast( + *j.template get_ptr()); + break; + case value_t::number_float: + val = static_cast( + *j.template get_ptr()); + break; + default: + JSON_THROW( + std::domain_error("type must be number, but is " + type_name(j))); } } @@ -752,32 +753,37 @@ template < typename Json, typename ArithmeticType, enable_if_t < std::is_arithmetic::value and - not std::is_same::value and - not std::is_same::value and - not std::is_same::value and + not std::is_same::value and + not std::is_same::value and + not std::is_same::value and not std::is_same::value, int > = 0 > -void from_json(const Json& j, ArithmeticType& val) +void from_json(const Json& j, ArithmeticType& val) { - if (j.is_number_unsigned()) + switch (static_cast(j)) { - val = static_cast(*j.template get_ptr()); - } - else if (j.is_number_integer()) - { - val = static_cast(*j.template get_ptr()); - } - else if (j.is_number_float()) - { - val = static_cast(*j.template get_ptr()); - } - else if (j.is_boolean()) - { - val = static_cast(*j.template get_ptr()); - } - else - { - JSON_THROW(std::domain_error("type must be number, but is " + type_name(j))); + case value_t::number_unsigned: + val = static_cast( + *j.template get_ptr()); + break; + case value_t::number_integer: + val = static_cast( + *j.template get_ptr()); + break; + case value_t::number_float: + val = static_cast( + *j.template get_ptr()); + break; + case value_t::boolean: + val = static_cast( + *j.template get_ptr()); + break; + default: + JSON_THROW( + std::domain_error("type must be number, but is " + type_name(j))); } } diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 710f61e8..c400e156 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -498,22 +498,23 @@ template = 0> void get_arithmetic_value(const Json& j, ArithmeticType& val) { - // unsigned must be checked first, since is_number_integer() == true for unsigned - if (j.is_number_unsigned()) + switch (static_cast(j)) { - val = static_cast(*j.template get_ptr()); - } - else if (j.is_number_integer()) - { - val = static_cast(*j.template get_ptr()); - } - else if (j.is_number_float()) - { - val = static_cast(*j.template get_ptr()); - } - else - { - JSON_THROW(std::domain_error("type must be number, but is " + type_name(j))); + case value_t::number_unsigned: + val = static_cast( + *j.template get_ptr()); + break; + case value_t::number_integer: + val = static_cast( + *j.template get_ptr()); + break; + case value_t::number_float: + val = static_cast( + *j.template get_ptr()); + break; + default: + JSON_THROW( + std::domain_error("type must be number, but is " + type_name(j))); } } @@ -752,32 +753,37 @@ template < typename Json, typename ArithmeticType, enable_if_t < std::is_arithmetic::value and - not std::is_same::value and - not std::is_same::value and - not std::is_same::value and + not std::is_same::value and + not std::is_same::value and + not std::is_same::value and not std::is_same::value, int > = 0 > -void from_json(const Json& j, ArithmeticType& val) +void from_json(const Json& j, ArithmeticType& val) { - if (j.is_number_unsigned()) + switch (static_cast(j)) { - val = static_cast(*j.template get_ptr()); - } - else if (j.is_number_integer()) - { - val = static_cast(*j.template get_ptr()); - } - else if (j.is_number_float()) - { - val = static_cast(*j.template get_ptr()); - } - else if (j.is_boolean()) - { - val = static_cast(*j.template get_ptr()); - } - else - { - JSON_THROW(std::domain_error("type must be number, but is " + type_name(j))); + case value_t::number_unsigned: + val = static_cast( + *j.template get_ptr()); + break; + case value_t::number_integer: + val = static_cast( + *j.template get_ptr()); + break; + case value_t::number_float: + val = static_cast( + *j.template get_ptr()); + break; + case value_t::boolean: + val = static_cast( + *j.template get_ptr()); + break; + default: + JSON_THROW( + std::domain_error("type must be number, but is " + type_name(j))); } }