Skip to content

Commit 8fb2005

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 8fb2005

3 files changed

Lines changed: 4 additions & 12 deletions

File tree

include/nlohmann/detail/conversions/from_json.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ 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+
#if defined(__cpp_lib_char8_t) && (__cpp_lib_char8_t >= 201907L)
544544
p = std_fs::path(std::u8string_view(reinterpret_cast<const char8_t*>(s.data()), s.size()));
545545
#else
546546
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: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5325,7 +5325,7 @@ 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+
#if defined(__cpp_lib_char8_t) && (__cpp_lib_char8_t >= 201907L)
53295329
p = std_fs::path(std::u8string_view(reinterpret_cast<const char8_t*>(s.data()), s.size()));
53305330
#else
53315331
p = std_fs::u8path(s); // accepts UTF-8 encoded std::string in C++17, deprecated in C++20
@@ -6090,12 +6090,8 @@ inline void to_json(BasicJsonType& j, const T& t)
60906090
template<typename BasicJsonType>
60916091
inline void to_json(BasicJsonType& j, const std_fs::path& p)
60926092
{
6093-
#ifdef JSON_HAS_CPP_20
6094-
const std::u8string s = p.u8string();
6093+
const auto& s = p.u8string();
60956094
j = std::string(s.begin(), s.end());
6096-
#else
6097-
j = p.u8string(); // returns std::string in C++17
6098-
#endif
60996095
}
61006096
#endif
61016097

0 commit comments

Comments
 (0)