Skip to content

Commit

Permalink
🚑 fix for #1169
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Jul 21, 2018
1 parent 04372a8 commit 347e77b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
10 changes: 10 additions & 0 deletions include/nlohmann/detail/conversions/from_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ namespace nlohmann
{
namespace detail
{
template<typename BasicJsonType>
void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
{
if (JSON_UNLIKELY(not j.is_null()))
{
JSON_THROW(type_error::create(302, "type must be null, but is " + std::string(j.type_name())));
}
n = nullptr;
}

// overloads for basic_json template parameters
template<typename BasicJsonType, typename ArithmeticType,
enable_if_t<std::is_arithmetic<ArithmeticType>::value and
Expand Down
10 changes: 10 additions & 0 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,16 @@ namespace nlohmann
{
namespace detail
{
template<typename BasicJsonType>
void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
{
if (JSON_UNLIKELY(not j.is_null()))
{
JSON_THROW(type_error::create(302, "type must be null, but is " + std::string(j.type_name())));
}
n = nullptr;
}

// overloads for basic_json template parameters
template<typename BasicJsonType, typename ArithmeticType,
enable_if_t<std::is_arithmetic<ArithmeticType>::value and
Expand Down
42 changes: 42 additions & 0 deletions test/src/unit-conversions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,48 @@ TEST_CASE("value conversion")
#endif
}

SECTION("get null (implicit)")
{
std::nullptr_t n;
json j(n);

std::nullptr_t n2 = j;
CHECK(n2 == n);
}

SECTION("get null (explicit)")
{
std::nullptr_t n;
json j(n);

auto n2 = j.get<std::nullptr_t>();
CHECK(n2 == n);

CHECK_THROWS_AS(json(json::value_t::string).get<std::nullptr_t>(), json::type_error&);
CHECK_THROWS_AS(json(json::value_t::object).get<std::nullptr_t>(), json::type_error&);
CHECK_THROWS_AS(json(json::value_t::array).get<std::nullptr_t>(), json::type_error&);
CHECK_THROWS_AS(json(json::value_t::boolean).get<std::nullptr_t>(), json::type_error&);
CHECK_THROWS_AS(json(json::value_t::number_integer).get<std::nullptr_t>(), json::type_error&);
CHECK_THROWS_AS(json(json::value_t::number_unsigned).get<std::nullptr_t>(), json::type_error&);
CHECK_THROWS_AS(json(json::value_t::number_float).get<std::nullptr_t>(), json::type_error&);

CHECK_THROWS_WITH(json(json::value_t::string).get<std::nullptr_t>(),
"[json.exception.type_error.302] type must be null, but is string");
CHECK_THROWS_WITH(json(json::value_t::object).get<std::nullptr_t>(),
"[json.exception.type_error.302] type must be null, but is object");
CHECK_THROWS_WITH(json(json::value_t::array).get<std::nullptr_t>(),
"[json.exception.type_error.302] type must be null, but is array");
CHECK_THROWS_WITH(json(json::value_t::boolean).get<std::nullptr_t>(),
"[json.exception.type_error.302] type must be null, but is boolean");
CHECK_THROWS_WITH(json(json::value_t::number_integer).get<std::nullptr_t>(),
"[json.exception.type_error.302] type must be null, but is number");
CHECK_THROWS_WITH(json(json::value_t::number_unsigned).get<std::nullptr_t>(),
"[json.exception.type_error.302] type must be null, but is number");
CHECK_THROWS_WITH(json(json::value_t::number_float).get<std::nullptr_t>(),
"[json.exception.type_error.302] type must be null, but is number");

}

SECTION("get a string (implicit)")
{
json::string_t s_reference{"Hello world"};
Expand Down

0 comments on commit 347e77b

Please sign in to comment.