| 1 |
From 756ca22ec5b0d89b5d107b4c30891d1293650c87 Mon Sep 17 00:00:00 2001 |
| 2 |
From: Sergiu Deitsch <[email protected]> |
| 3 |
Date: Wed, 23 Apr 2025 18:36:41 +0200 |
| 4 |
Subject: [PATCH] Provide fallback for missing `char8_t` support (#4736) |
| 5 |
|
| 6 |
--- |
| 7 |
.../nlohmann/detail/conversions/from_json.hpp | 5 +++- |
| 8 |
.../nlohmann/detail/conversions/to_json.hpp | 21 ++++++++++----- |
| 9 |
single_include/nlohmann/json.hpp | 26 +++++++++++++------ |
| 10 |
tests/src/unit-deserialization.cpp | 5 ++-- |
| 11 |
4 files changed, 39 insertions(+), 18 deletions(-) |
| 12 |
|
| 13 |
diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp |
| 14 |
index 797f714dfa..3a24a6f4d2 100644 |
| 15 |
--- a/include/nlohmann/detail/conversions/from_json.hpp |
| 16 |
+++ b/include/nlohmann/detail/conversions/from_json.hpp |
| 17 |
@@ -539,7 +539,10 @@ inline void from_json(const BasicJsonType& j, std_fs::path& p) |
| 18 |
JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j)); |
| 19 |
} |
| 20 |
const auto& s = *j.template get_ptr<const typename BasicJsonType::string_t*>(); |
| 21 |
-#ifdef JSON_HAS_CPP_20 |
| 22 |
+ // Checking for C++20 standard or later can be insufficient in case the |
| 23 |
+ // library support for char8_t is either incomplete or was disabled |
| 24 |
+ // altogether. Use the __cpp_lib_char8_t feature test instead. |
| 25 |
+#if defined(__cpp_lib_char8_t) && (__cpp_lib_char8_t >= 201907L) |
| 26 |
p = std_fs::path(std::u8string_view(reinterpret_cast<const char8_t*>(s.data()), s.size())); |
| 27 |
#else |
| 28 |
p = std_fs::u8path(s); // accepts UTF-8 encoded std::string in C++17, deprecated in C++20 |
| 29 |
diff --git a/include/nlohmann/detail/conversions/to_json.hpp b/include/nlohmann/detail/conversions/to_json.hpp |
| 30 |
index f8413850d5..8b910dd161 100644 |
| 31 |
--- a/include/nlohmann/detail/conversions/to_json.hpp |
| 32 |
+++ b/include/nlohmann/detail/conversions/to_json.hpp |
| 33 |
@@ -15,7 +15,8 @@ |
| 34 |
|
| 35 |
#include <algorithm> // copy |
| 36 |
#include <iterator> // begin, end |
| 37 |
-#include <string> // string |
| 38 |
+#include <memory> // allocator_traits |
| 39 |
+#include <string> // basic_string, char_traits |
| 40 |
#include <tuple> // tuple, get |
| 41 |
#include <type_traits> // is_same, is_constructible, is_floating_point, is_enum, underlying_type |
| 42 |
#include <utility> // move, forward, declval, pair |
| 43 |
@@ -440,15 +441,21 @@ inline void to_json(BasicJsonType& j, const T& t) |
| 44 |
} |
| 45 |
|
| 46 |
#if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM |
| 47 |
+#if defined(__cpp_lib_char8_t) |
| 48 |
+template<typename BasicJsonType, typename Tr, typename Allocator> |
| 49 |
+inline void to_json(BasicJsonType& j, const std::basic_string<char8_t, Tr, Allocator>& s) |
| 50 |
+{ |
| 51 |
+ using OtherAllocator = typename std::allocator_traits<Allocator>::template rebind_alloc<char>; |
| 52 |
+ j = std::basic_string<char, std::char_traits<char>, OtherAllocator>(s.begin(), s.end(), s.get_allocator()); |
| 53 |
+} |
| 54 |
+#endif |
| 55 |
+ |
| 56 |
template<typename BasicJsonType> |
| 57 |
inline void to_json(BasicJsonType& j, const std_fs::path& p) |
| 58 |
{ |
| 59 |
-#ifdef JSON_HAS_CPP_20 |
| 60 |
- const std::u8string s = p.u8string(); |
| 61 |
- j = std::string(s.begin(), s.end()); |
| 62 |
-#else |
| 63 |
- j = p.u8string(); // returns std::string in C++17 |
| 64 |
-#endif |
| 65 |
+ // Returns either a std::string or a std::u8string depending whether library |
| 66 |
+ // support for char8_t is enabled. |
| 67 |
+ j = p.u8string(); |
| 68 |
} |
| 69 |
#endif |
| 70 |
|
| 71 |
diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp |
| 72 |
index 13b07c0fbc..93e5983cf1 100644 |
| 73 |
--- a/single_include/nlohmann/json.hpp |
| 74 |
+++ b/single_include/nlohmann/json.hpp |
| 75 |
@@ -5324,7 +5324,10 @@ inline void from_json(const BasicJsonType& j, std_fs::path& p) |
| 76 |
JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j)); |
| 77 |
} |
| 78 |
const auto& s = *j.template get_ptr<const typename BasicJsonType::string_t*>(); |
| 79 |
-#ifdef JSON_HAS_CPP_20 |
| 80 |
+ // Checking for C++20 standard or later can be insufficient in case the |
| 81 |
+ // library support for char8_t is either incomplete or was disabled |
| 82 |
+ // altogether. Use the __cpp_lib_char8_t feature test instead. |
| 83 |
+#if defined(__cpp_lib_char8_t) && (__cpp_lib_char8_t >= 201907L) |
| 84 |
p = std_fs::path(std::u8string_view(reinterpret_cast<const char8_t*>(s.data()), s.size())); |
| 85 |
#else |
| 86 |
p = std_fs::u8path(s); // accepts UTF-8 encoded std::string in C++17, deprecated in C++20 |
| 87 |
@@ -5379,7 +5382,8 @@ NLOHMANN_JSON_NAMESPACE_END |
| 88 |
|
| 89 |
#include <algorithm> // copy |
| 90 |
#include <iterator> // begin, end |
| 91 |
-#include <string> // string |
| 92 |
+#include <memory> // allocator_traits |
| 93 |
+#include <string> // basic_string, char_traits |
| 94 |
#include <tuple> // tuple, get |
| 95 |
#include <type_traits> // is_same, is_constructible, is_floating_point, is_enum, underlying_type |
| 96 |
#include <utility> // move, forward, declval, pair |
| 97 |
@@ -6086,15 +6090,21 @@ inline void to_json(BasicJsonType& j, const T& t) |
| 98 |
} |
| 99 |
|
| 100 |
#if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM |
| 101 |
+#if defined(__cpp_lib_char8_t) |
| 102 |
+template<typename BasicJsonType, typename Tr, typename Allocator> |
| 103 |
+inline void to_json(BasicJsonType& j, const std::basic_string<char8_t, Tr, Allocator>& s) |
| 104 |
+{ |
| 105 |
+ using OtherAllocator = typename std::allocator_traits<Allocator>::template rebind_alloc<char>; |
| 106 |
+ j = std::basic_string<char, std::char_traits<char>, OtherAllocator>(s.begin(), s.end(), s.get_allocator()); |
| 107 |
+} |
| 108 |
+#endif |
| 109 |
+ |
| 110 |
template<typename BasicJsonType> |
| 111 |
inline void to_json(BasicJsonType& j, const std_fs::path& p) |
| 112 |
{ |
| 113 |
-#ifdef JSON_HAS_CPP_20 |
| 114 |
- const std::u8string s = p.u8string(); |
| 115 |
- j = std::string(s.begin(), s.end()); |
| 116 |
-#else |
| 117 |
- j = p.u8string(); // returns std::string in C++17 |
| 118 |
-#endif |
| 119 |
+ // Returns either a std::string or a std::u8string depending whether library |
| 120 |
+ // support for char8_t is enabled. |
| 121 |
+ j = p.u8string(); |
| 122 |
} |
| 123 |
#endif |
| 124 |
|
| 125 |
diff --git a/tests/src/unit-deserialization.cpp b/tests/src/unit-deserialization.cpp |
| 126 |
index 84a970a183..5c450c23d3 100644 |
| 127 |
--- a/tests/src/unit-deserialization.cpp |
| 128 |
+++ b/tests/src/unit-deserialization.cpp |
| 129 |
@@ -1134,9 +1134,10 @@ TEST_CASE("deserialization") |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
-// select the types to test - char8_t is only available in C++20 |
| 134 |
+// select the types to test - char8_t is only available since C++20 if and only |
| 135 |
+// if __cpp_char8_t is defined. |
| 136 |
#define TYPE_LIST(...) __VA_ARGS__ |
| 137 |
-#ifdef JSON_HAS_CPP_20 |
| 138 |
+#if defined(__cpp_char8_t) && (__cpp_char8_t >= 201811L) |
| 139 |
#define ASCII_TYPES TYPE_LIST(char, wchar_t, char16_t, char32_t, char8_t) |
| 140 |
#else |
| 141 |
#define ASCII_TYPES TYPE_LIST(char, wchar_t, char16_t, char32_t) |