Skip to content

Commit 955d205

Browse files
committed
Provide fallback for missing char8_t support
Clang 11.0.x with libc++ fails to compile tests in C++20 mode due to incomplete char8_t support in std::filesystem::path. Signed-off-by: Sergiu Deitsch <sergiu.deitsch@gmail.com>
1 parent 7ddea26 commit 955d205

3 files changed

Lines changed: 10 additions & 12 deletions

File tree

include/nlohmann/detail/conversions/from_json.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,10 @@ inline void from_json(const BasicJsonType& j, std_fs::path& p)
540540
JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
541541
}
542542
const auto& s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
543-
#ifdef JSON_HAS_CPP_20
543+
// Checking for C++20 standard or later can be insufficient in case the
544+
// library support for char8_t is either incomplete or was disabled
545+
// altogether. Use the __cpp_lib_char8_t feature test instead.
546+
#if defined(__cpp_lib_char8_t) && (__cpp_lib_char8_t >= 201907L)
544547
p = std_fs::path(std::u8string_view(reinterpret_cast<const char8_t*>(s.data()), s.size()));
545548
#else
546549
p = std_fs::u8path(s); // accepts UTF-8 encoded std::string in C++17, deprecated in C++20

include/nlohmann/detail/conversions/to_json.hpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -443,12 +443,8 @@ inline void to_json(BasicJsonType& j, const T& t)
443443
template<typename BasicJsonType>
444444
inline void to_json(BasicJsonType& j, const std_fs::path& p)
445445
{
446-
#ifdef JSON_HAS_CPP_20
447-
const std::u8string s = p.u8string();
446+
const auto& s = p.u8string();
448447
j = std::string(s.begin(), s.end());
449-
#else
450-
j = p.u8string(); // returns std::string in C++17
451-
#endif
452448
}
453449
#endif
454450

single_include/nlohmann/json.hpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5325,7 +5325,10 @@ inline void from_json(const BasicJsonType& j, std_fs::path& p)
53255325
JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
53265326
}
53275327
const auto& s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
5328-
#ifdef JSON_HAS_CPP_20
5328+
// Checking for C++20 standard or later can be insufficient in case the
5329+
// library support for char8_t is either incomplete or was disabled
5330+
// altogether. Use the __cpp_lib_char8_t feature test instead.
5331+
#if defined(__cpp_lib_char8_t) && (__cpp_lib_char8_t >= 201907L)
53295332
p = std_fs::path(std::u8string_view(reinterpret_cast<const char8_t*>(s.data()), s.size()));
53305333
#else
53315334
p = std_fs::u8path(s); // accepts UTF-8 encoded std::string in C++17, deprecated in C++20
@@ -6090,12 +6093,8 @@ inline void to_json(BasicJsonType& j, const T& t)
60906093
template<typename BasicJsonType>
60916094
inline void to_json(BasicJsonType& j, const std_fs::path& p)
60926095
{
6093-
#ifdef JSON_HAS_CPP_20
6094-
const std::u8string s = p.u8string();
6096+
const auto& s = p.u8string();
60956097
j = std::string(s.begin(), s.end());
6096-
#else
6097-
j = p.u8string(); // returns std::string in C++17
6098-
#endif
60996098
}
61006099
#endif
61016100

0 commit comments

Comments
 (0)