1
0
mirror of https://github.com/nlohmann/json synced 2024-11-30 23:50:08 +00:00

use switchs where appropriate

This commit is contained in:
Théo DELRIEU 2017-01-20 23:21:20 +01:00
parent 1e20887cff
commit d566bb81c4
2 changed files with 86 additions and 74 deletions

View File

@ -498,22 +498,23 @@ template <typename Json, typename ArithmeticType,
int> = 0> int> = 0>
void get_arithmetic_value(const Json& j, ArithmeticType& val) void get_arithmetic_value(const Json& j, ArithmeticType& val)
{ {
// unsigned must be checked first, since is_number_integer() == true for unsigned switch (static_cast<value_t>(j))
if (j.is_number_unsigned())
{ {
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::number_unsigned_t*>()); case value_t::number_unsigned:
} val = static_cast<ArithmeticType>(
else if (j.is_number_integer()) *j.template get_ptr<const typename Json::number_unsigned_t*>());
{ break;
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::number_integer_t*>()); case value_t::number_integer:
} val = static_cast<ArithmeticType>(
else if (j.is_number_float()) *j.template get_ptr<const typename Json::number_integer_t*>());
{ break;
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::number_float_t*>()); case value_t::number_float:
} val = static_cast<ArithmeticType>(
else *j.template get_ptr<const typename Json::number_float_t*>());
{ break;
JSON_THROW(std::domain_error("type must be number, but is " + type_name(j))); default:
JSON_THROW(
std::domain_error("type must be number, but is " + type_name(j)));
} }
} }
@ -752,32 +753,37 @@ template <
typename Json, typename ArithmeticType, typename Json, typename ArithmeticType,
enable_if_t < enable_if_t <
std::is_arithmetic<ArithmeticType>::value and std::is_arithmetic<ArithmeticType>::value and
not std::is_same<ArithmeticType, typename Json::number_unsigned_t>::value and not std::is_same<ArithmeticType,
not std::is_same<ArithmeticType, typename Json::number_integer_t>::value and typename Json::number_unsigned_t>::value and
not std::is_same<ArithmeticType, typename Json::number_float_t>::value and not std::is_same<ArithmeticType,
typename Json::number_integer_t>::value and
not std::is_same<ArithmeticType,
typename Json::number_float_t>::value and
not std::is_same<ArithmeticType, typename Json::boolean_t>::value, not std::is_same<ArithmeticType, typename Json::boolean_t>::value,
int > = 0 > 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<value_t>(j))
{ {
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::number_unsigned_t*>()); case value_t::number_unsigned:
} val = static_cast<ArithmeticType>(
else if (j.is_number_integer()) *j.template get_ptr<const typename Json::number_unsigned_t*>());
{ break;
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::number_integer_t*>()); case value_t::number_integer:
} val = static_cast<ArithmeticType>(
else if (j.is_number_float()) *j.template get_ptr<const typename Json::number_integer_t*>());
{ break;
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::number_float_t*>()); case value_t::number_float:
} val = static_cast<ArithmeticType>(
else if (j.is_boolean()) *j.template get_ptr<const typename Json::number_float_t*>());
{ break;
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::boolean_t*>()); case value_t::boolean:
} val = static_cast<ArithmeticType>(
else *j.template get_ptr<const typename Json::boolean_t*>());
{ break;
JSON_THROW(std::domain_error("type must be number, but is " + type_name(j))); default:
JSON_THROW(
std::domain_error("type must be number, but is " + type_name(j)));
} }
} }

View File

@ -498,22 +498,23 @@ template <typename Json, typename ArithmeticType,
int> = 0> int> = 0>
void get_arithmetic_value(const Json& j, ArithmeticType& val) void get_arithmetic_value(const Json& j, ArithmeticType& val)
{ {
// unsigned must be checked first, since is_number_integer() == true for unsigned switch (static_cast<value_t>(j))
if (j.is_number_unsigned())
{ {
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::number_unsigned_t*>()); case value_t::number_unsigned:
} val = static_cast<ArithmeticType>(
else if (j.is_number_integer()) *j.template get_ptr<const typename Json::number_unsigned_t*>());
{ break;
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::number_integer_t*>()); case value_t::number_integer:
} val = static_cast<ArithmeticType>(
else if (j.is_number_float()) *j.template get_ptr<const typename Json::number_integer_t*>());
{ break;
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::number_float_t*>()); case value_t::number_float:
} val = static_cast<ArithmeticType>(
else *j.template get_ptr<const typename Json::number_float_t*>());
{ break;
JSON_THROW(std::domain_error("type must be number, but is " + type_name(j))); default:
JSON_THROW(
std::domain_error("type must be number, but is " + type_name(j)));
} }
} }
@ -752,32 +753,37 @@ template <
typename Json, typename ArithmeticType, typename Json, typename ArithmeticType,
enable_if_t < enable_if_t <
std::is_arithmetic<ArithmeticType>::value and std::is_arithmetic<ArithmeticType>::value and
not std::is_same<ArithmeticType, typename Json::number_unsigned_t>::value and not std::is_same<ArithmeticType,
not std::is_same<ArithmeticType, typename Json::number_integer_t>::value and typename Json::number_unsigned_t>::value and
not std::is_same<ArithmeticType, typename Json::number_float_t>::value and not std::is_same<ArithmeticType,
typename Json::number_integer_t>::value and
not std::is_same<ArithmeticType,
typename Json::number_float_t>::value and
not std::is_same<ArithmeticType, typename Json::boolean_t>::value, not std::is_same<ArithmeticType, typename Json::boolean_t>::value,
int > = 0 > 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<value_t>(j))
{ {
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::number_unsigned_t*>()); case value_t::number_unsigned:
} val = static_cast<ArithmeticType>(
else if (j.is_number_integer()) *j.template get_ptr<const typename Json::number_unsigned_t*>());
{ break;
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::number_integer_t*>()); case value_t::number_integer:
} val = static_cast<ArithmeticType>(
else if (j.is_number_float()) *j.template get_ptr<const typename Json::number_integer_t*>());
{ break;
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::number_float_t*>()); case value_t::number_float:
} val = static_cast<ArithmeticType>(
else if (j.is_boolean()) *j.template get_ptr<const typename Json::number_float_t*>());
{ break;
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::boolean_t*>()); case value_t::boolean:
} val = static_cast<ArithmeticType>(
else *j.template get_ptr<const typename Json::boolean_t*>());
{ break;
JSON_THROW(std::domain_error("type must be number, but is " + type_name(j))); default:
JSON_THROW(
std::domain_error("type must be number, but is " + type_name(j)));
} }
} }