diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index 11e40f5f4..6ecf7e657 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -167,9 +167,13 @@ template::value, int> = 0> inline void from_json(const BasicJsonType& j, EnumType& e) { - typename std::underlying_type::type val; + using underlying_type = typename std::underlying_type::type; + // get_arithmetic_value() does not accept boolean_t; read the number that to_json() wrote instead + using value_type = typename std::conditional::value, + typename BasicJsonType::number_unsigned_t, underlying_type>::type; + value_type val; get_arithmetic_value(j, val); - e = static_cast(val); + e = static_cast(static_cast(val)); } #endif // JSON_DISABLE_ENUM_SERIALIZATION diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 73c8ab9be..9a9d6ae0c 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -5664,9 +5664,13 @@ template::value, int> = 0> inline void from_json(const BasicJsonType& j, EnumType& e) { - typename std::underlying_type::type val; + using underlying_type = typename std::underlying_type::type; + // get_arithmetic_value() does not accept boolean_t; read the number that to_json() wrote instead + using value_type = typename std::conditional::value, + typename BasicJsonType::number_unsigned_t, underlying_type>::type; + value_type val; get_arithmetic_value(j, val); - e = static_cast(val); + e = static_cast(static_cast(val)); } #endif // JSON_DISABLE_ENUM_SERIALIZATION diff --git a/tests/src/unit-conversions.cpp b/tests/src/unit-conversions.cpp index 90d972f71..077ba0e14 100644 --- a/tests/src/unit-conversions.cpp +++ b/tests/src/unit-conversions.cpp @@ -1358,6 +1358,14 @@ TEST_CASE("value conversion") CHECK(json(value_1).get() == value_1); CHECK(json(cpp_enum::value_1).get() == cpp_enum::value_1); } + + SECTION("get an enum with underlying type bool (#5671)") + { + enum class bool_enum : bool { off, on }; + + CHECK(json(bool_enum::off).get() == bool_enum::off); + CHECK(json(bool_enum::on).get() == bool_enum::on); + } #endif SECTION("more involved conversions")