From d90b6bc721fd7d992023baa5fcc1b300c814db7c Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 20 Oct 2021 13:52:28 +0200 Subject: [PATCH] :alembic: use proper header for filesystem --- .clang-tidy | 1 + .../nlohmann/detail/conversions/from_json.hpp | 16 +++++- .../nlohmann/detail/conversions/to_json.hpp | 16 +++++- include/nlohmann/detail/macro_scope.hpp | 17 +++++++ include/nlohmann/detail/macro_unscope.hpp | 1 + single_include/nlohmann/json.hpp | 50 +++++++++++++++++-- test/src/unit-regression2.cpp | 20 ++++++-- 7 files changed, 109 insertions(+), 12 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 33cf84821..419ef470f 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -30,6 +30,7 @@ Checks: '*, -llvmlibc-*, -misc-no-recursion, -misc-non-private-member-variables-in-classes, + -modernize-use-nodiscard, -modernize-use-trailing-return-type, -readability-function-size, -readability-function-cognitive-complexity, diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index 71e32aaf7..e2fb89f26 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -20,7 +20,19 @@ #include #ifdef JSON_HAS_CPP_17 - #include +#if JSON_STD_FILESYSTEM_EXPERIMENTAL +#include +namespace nlohmann::detail +{ +namespace std_fs = std::experimental::filesystem; +} +#else +#include +namespace nlohmann::detail +{ +namespace std_fs = std::filesystem; +} +#endif #endif namespace nlohmann @@ -450,7 +462,7 @@ void from_json(const BasicJsonType& j, std::unordered_map -void from_json(const BasicJsonType& j, std::filesystem::path& p) +void from_json(const BasicJsonType& j, std_fs::path& p) { if (JSON_HEDLEY_UNLIKELY(!j.is_string())) { diff --git a/include/nlohmann/detail/conversions/to_json.hpp b/include/nlohmann/detail/conversions/to_json.hpp index 79fdb3233..c11213114 100644 --- a/include/nlohmann/detail/conversions/to_json.hpp +++ b/include/nlohmann/detail/conversions/to_json.hpp @@ -16,7 +16,19 @@ #include #ifdef JSON_HAS_CPP_17 - #include +#if JSON_STD_FILESYSTEM_EXPERIMENTAL +#include +namespace nlohmann::detail +{ +namespace std_fs = std::experimental::filesystem; +} +#else +#include +namespace nlohmann::detail +{ +namespace std_fs = std::filesystem; +} +#endif #endif namespace nlohmann @@ -393,7 +405,7 @@ void to_json(BasicJsonType& j, const T& t) #ifdef JSON_HAS_CPP_17 template -void to_json(BasicJsonType& j, const std::filesystem::path& p) +void to_json(BasicJsonType& j, const std_fs::path& p) { j = p.string(); } diff --git a/include/nlohmann/detail/macro_scope.hpp b/include/nlohmann/detail/macro_scope.hpp index c2a65bdda..f080db195 100644 --- a/include/nlohmann/detail/macro_scope.hpp +++ b/include/nlohmann/detail/macro_scope.hpp @@ -37,6 +37,23 @@ #define JSON_HAS_CPP_11 #endif +#ifdef JSON_HAS_CPP_17 + // set JSON_STD_FILESYSTEM_EXPERIMENTAL to 1 if should be taken instead of + #if defined(__cpp_lib_filesystem) + #define JSON_STD_FILESYSTEM_EXPERIMENTAL 0 + #elif defined(__cpp_lib_experimental_filesystem) + #define JSON_STD_FILESYSTEM_EXPERIMENTAL 1 + #elif !defined(__has_include) + #define JSON_STD_FILESYSTEM_EXPERIMENTAL 1 + #elif __has_include() + #define JSON_STD_FILESYSTEM_EXPERIMENTAL 0 + #elif __has_include() + #define JSON_STD_FILESYSTEM_EXPERIMENTAL 1 + #else + #define JSON_STD_FILESYSTEM_EXPERIMENTAL 0 + #endif +#endif + // disable documentation warnings on clang #if defined(__clang__) #pragma clang diagnostic push diff --git a/include/nlohmann/detail/macro_unscope.hpp b/include/nlohmann/detail/macro_unscope.hpp index 67bf1466b..e0cc5dc40 100644 --- a/include/nlohmann/detail/macro_unscope.hpp +++ b/include/nlohmann/detail/macro_unscope.hpp @@ -16,6 +16,7 @@ #undef JSON_HAS_CPP_14 #undef JSON_HAS_CPP_17 #undef JSON_HAS_CPP_20 +#undef JSON_STD_FILESYSTEM_EXPERIMENTAL #undef NLOHMANN_BASIC_JSON_TPL_DECLARATION #undef NLOHMANN_BASIC_JSON_TPL #undef JSON_EXPLICIT diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 87475ab31..3d77c72a7 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2325,6 +2325,23 @@ using is_detected_convertible = #define JSON_HAS_CPP_11 #endif +#ifdef JSON_HAS_CPP_17 + // set JSON_STD_FILESYSTEM_EXPERIMENTAL to 1 if should be taken instead of + #if defined(__cpp_lib_filesystem) + #define JSON_STD_FILESYSTEM_EXPERIMENTAL 0 + #elif defined(__cpp_lib_experimental_filesystem) + #define JSON_STD_FILESYSTEM_EXPERIMENTAL 1 + #elif !defined(__has_include) + #define JSON_STD_FILESYSTEM_EXPERIMENTAL 1 + #elif __has_include() + #define JSON_STD_FILESYSTEM_EXPERIMENTAL 0 + #elif __has_include() + #define JSON_STD_FILESYSTEM_EXPERIMENTAL 1 + #else + #define JSON_STD_FILESYSTEM_EXPERIMENTAL 0 + #endif +#endif + // disable documentation warnings on clang #if defined(__clang__) #pragma clang diagnostic push @@ -3951,7 +3968,19 @@ T conditional_static_cast(U value) #ifdef JSON_HAS_CPP_17 - #include +#if JSON_STD_FILESYSTEM_EXPERIMENTAL +#include +namespace nlohmann::detail +{ +namespace std_fs = std::experimental::filesystem; +} +#else +#include +namespace nlohmann::detail +{ +namespace std_fs = std::filesystem; +} +#endif #endif namespace nlohmann @@ -4381,7 +4410,7 @@ void from_json(const BasicJsonType& j, std::unordered_map -void from_json(const BasicJsonType& j, std::filesystem::path& p) +void from_json(const BasicJsonType& j, std_fs::path& p) { if (JSON_HEDLEY_UNLIKELY(!j.is_string())) { @@ -4627,7 +4656,19 @@ class tuple_element> #ifdef JSON_HAS_CPP_17 - #include +#if JSON_STD_FILESYSTEM_EXPERIMENTAL +#include +namespace nlohmann::detail +{ +namespace std_fs = std::experimental::filesystem; +} +#else +#include +namespace nlohmann::detail +{ +namespace std_fs = std::filesystem; +} +#endif #endif namespace nlohmann @@ -5004,7 +5045,7 @@ void to_json(BasicJsonType& j, const T& t) #ifdef JSON_HAS_CPP_17 template -void to_json(BasicJsonType& j, const std::filesystem::path& p) +void to_json(BasicJsonType& j, const std_fs::path& p) { j = p.string(); } @@ -26591,6 +26632,7 @@ inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std #undef JSON_HAS_CPP_14 #undef JSON_HAS_CPP_17 #undef JSON_HAS_CPP_20 +#undef JSON_STD_FILESYSTEM_EXPERIMENTAL #undef NLOHMANN_BASIC_JSON_TPL_DECLARATION #undef NLOHMANN_BASIC_JSON_TPL #undef JSON_EXPLICIT diff --git a/test/src/unit-regression2.cpp b/test/src/unit-regression2.cpp index f0c4ef451..07b21ff5e 100644 --- a/test/src/unit-regression2.cpp +++ b/test/src/unit-regression2.cpp @@ -47,8 +47,20 @@ using ordered_json = nlohmann::ordered_json; #endif #ifdef JSON_HAS_CPP_17 - #include - #include +#include +#if JSON_STD_FILESYSTEM_EXPERIMENTAL +#include +namespace nlohmann::detail +{ +namespace std_fs = std::experimental::filesystem; +} +#else +#include +namespace nlohmann::detail +{ +namespace std_fs = std::filesystem; +} +#endif #endif #ifdef JSON_HAS_CPP_20 @@ -731,10 +743,10 @@ TEST_CASE("regression tests 2") #ifdef JSON_HAS_CPP_17 SECTION("issue #3070 - Version 3.10.3 breaks backward-compatibility with 3.10.2 ") { - std::filesystem::path text_path("/tmp/text.txt"); + nlohmann::detail::std_fs::path text_path("/tmp/text.txt"); json j(text_path); - const auto j_path = j.get(); + const auto j_path = j.get(); CHECK(j_path == text_path); } #endif