Skip to content

Commit

Permalink
Restore disabled check for nlohmann#3070 (except on MSVC)
Browse files Browse the repository at this point in the history
Restore the previously disabled check for regression nlohmann#3070 on all
compilers but MSVC.

To summarize the issue:
Given namespace fs = std::filesystem.
On MSVC attempting to construct an fs::path from json results in an
ambiguous overload resolution because fs::path can be constructed from
both a std::string as well as another fs::path.
To the best of my knowledge there is no way to fix an ambiguous overload
situation involving a type we do not control and with json implicitly
converting to both std::string and fs::path.

Re-enabling the check where it compiles and keeping it disabled for MSVC
is the best we can do.

Closes nlohmann#3377 and nlohmann#3382.
  • Loading branch information
falbrechtskirchinger committed Apr 5, 2022
1 parent ab5cecb commit db1196f
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
3 changes: 1 addition & 2 deletions include/nlohmann/detail/conversions/from_json.hpp
Expand Up @@ -464,8 +464,7 @@ void from_json(const BasicJsonType& j, std_fs::path& p)
{
if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
{
// Not tested because of #3377 (related #3070)
JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), j)); // LCOV_EXCL_LINE
JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), j));
}
p = *j.template get_ptr<const typename BasicJsonType::string_t*>();
}
Expand Down
3 changes: 1 addition & 2 deletions single_include/nlohmann/json.hpp
Expand Up @@ -4279,8 +4279,7 @@ void from_json(const BasicJsonType& j, std_fs::path& p)
{
if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
{
// Not tested because of #3377 (related #3070)
JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), j)); // LCOV_EXCL_LINE
JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), j));
}
p = *j.template get_ptr<const typename BasicJsonType::string_t*>();
}
Expand Down
6 changes: 4 additions & 2 deletions test/src/unit-regression2.cpp
Expand Up @@ -748,8 +748,10 @@ TEST_CASE("regression tests 2")
const auto j_path = j.get<nlohmann::detail::std_fs::path>();
CHECK(j_path == text_path);

// Disabled pending resolution of #3377
// CHECK_THROWS_WITH_AS(nlohmann::detail::std_fs::path(json(1)), "[json.exception.type_error.302] type must be string, but is number", json::type_error);
#ifndef _MSC_VER
// works everywhere but on MSVC
CHECK_THROWS_WITH_AS(nlohmann::detail::std_fs::path(json(1)), "[json.exception.type_error.302] type must be string, but is number", json::type_error);
#endif
}
#endif

Expand Down

0 comments on commit db1196f

Please sign in to comment.