From 5eddcc134df81791b455e7eb0e3d4ef3a623ce0e Mon Sep 17 00:00:00 2001 From: Alexander Karzhenkov Date: Fri, 21 Aug 2020 10:05:51 +0500 Subject: [PATCH] Allow to use string_view from std::experimental --- include/nlohmann/detail/macro_scope.hpp | 19 ++++++++++++ include/nlohmann/json.hpp | 2 +- single_include/nlohmann/json.hpp | 21 ++++++++++++- test/src/unit-conversions.cpp | 39 ++++++++++++------------- 4 files changed, 58 insertions(+), 23 deletions(-) diff --git a/include/nlohmann/detail/macro_scope.hpp b/include/nlohmann/detail/macro_scope.hpp index 77acf04c7..8dd34ac97 100644 --- a/include/nlohmann/detail/macro_scope.hpp +++ b/include/nlohmann/detail/macro_scope.hpp @@ -31,6 +31,25 @@ #define JSON_HAS_CPP_14 #endif +namespace nlohmann { +namespace std_aliases { } +using namespace std_aliases; +} + +#if defined(JSON_HAS_CPP_17) + #if __has_include() + #include + namespace nlohmann::std_aliases { + using std::string_view; + } + #elif __has_include() + #include + namespace nlohmann::std_aliases { + using std::experimental::string_view; + } + #endif +#endif + // disable float-equal warnings on GCC/clang #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) #pragma GCC diagnostic push diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index cd56dafdc..e9bb1afd4 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -3230,7 +3230,7 @@ class basic_json !detail::is_basic_json::value && !std::is_same>::value #if defined(JSON_HAS_CPP_17) && (defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER <= 1914)) - && !std::is_same::value + && !std::is_same::value #endif && detail::is_detected::value , int >::type = 0 > diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index e9dbc87da..2ba6c4051 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2048,6 +2048,25 @@ JSON_HEDLEY_DIAGNOSTIC_POP #define JSON_HAS_CPP_14 #endif +namespace nlohmann { +namespace std_aliases { } +using namespace std_aliases; +} + +#if defined(JSON_HAS_CPP_17) + #if __has_include() + #include + namespace nlohmann::std_aliases { + using std::string_view; + } + #elif __has_include() + #include + namespace nlohmann::std_aliases { + using std::experimental::string_view; + } + #endif +#endif + // disable float-equal warnings on GCC/clang #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) #pragma GCC diagnostic push @@ -19736,7 +19755,7 @@ class basic_json !detail::is_basic_json::value && !std::is_same>::value #if defined(JSON_HAS_CPP_17) && (defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER <= 1914)) - && !std::is_same::value + && !std::is_same::value #endif && detail::is_detected::value , int >::type = 0 > diff --git a/test/src/unit-conversions.cpp b/test/src/unit-conversions.cpp index c71e230d9..091c4130c 100644 --- a/test/src/unit-conversions.cpp +++ b/test/src/unit-conversions.cpp @@ -32,6 +32,7 @@ SOFTWARE. #define JSON_TESTS_PRIVATE #include using nlohmann::json; +using namespace nlohmann::std_aliases; #include #include @@ -48,10 +49,6 @@ using nlohmann::json; #define JSON_HAS_CPP_14 #endif -#if defined(JSON_HAS_CPP_17) - #include -#endif - TEST_CASE("value conversion") { SECTION("get an object (explicit)") @@ -465,7 +462,7 @@ TEST_CASE("value conversion") #if defined(JSON_HAS_CPP_17) SECTION("std::string_view") { - std::string_view s = j.get(); + string_view s = j.get(); CHECK(json(s) == j); } #endif @@ -514,27 +511,27 @@ TEST_CASE("value conversion") #if defined(JSON_HAS_CPP_17) SECTION("exception in case of a non-string type using string_view") { - CHECK_THROWS_AS(json(json::value_t::null).get(), json::type_error&); - CHECK_THROWS_AS(json(json::value_t::object).get(), json::type_error&); - CHECK_THROWS_AS(json(json::value_t::array).get(), json::type_error&); - CHECK_THROWS_AS(json(json::value_t::boolean).get(), json::type_error&); - CHECK_THROWS_AS(json(json::value_t::number_integer).get(), json::type_error&); - CHECK_THROWS_AS(json(json::value_t::number_unsigned).get(), json::type_error&); - CHECK_THROWS_AS(json(json::value_t::number_float).get(), json::type_error&); + CHECK_THROWS_AS(json(json::value_t::null).get(), json::type_error&); + CHECK_THROWS_AS(json(json::value_t::object).get(), json::type_error&); + CHECK_THROWS_AS(json(json::value_t::array).get(), json::type_error&); + CHECK_THROWS_AS(json(json::value_t::boolean).get(), json::type_error&); + CHECK_THROWS_AS(json(json::value_t::number_integer).get(), json::type_error&); + CHECK_THROWS_AS(json(json::value_t::number_unsigned).get(), json::type_error&); + CHECK_THROWS_AS(json(json::value_t::number_float).get(), json::type_error&); - CHECK_THROWS_WITH(json(json::value_t::null).get(), + CHECK_THROWS_WITH(json(json::value_t::null).get(), "[json.exception.type_error.302] type must be string, but is null"); - CHECK_THROWS_WITH(json(json::value_t::object).get(), + CHECK_THROWS_WITH(json(json::value_t::object).get(), "[json.exception.type_error.302] type must be string, but is object"); - CHECK_THROWS_WITH(json(json::value_t::array).get(), + CHECK_THROWS_WITH(json(json::value_t::array).get(), "[json.exception.type_error.302] type must be string, but is array"); - CHECK_THROWS_WITH(json(json::value_t::boolean).get(), + CHECK_THROWS_WITH(json(json::value_t::boolean).get(), "[json.exception.type_error.302] type must be string, but is boolean"); - CHECK_THROWS_WITH(json(json::value_t::number_integer).get(), + CHECK_THROWS_WITH(json(json::value_t::number_integer).get(), "[json.exception.type_error.302] type must be string, but is number"); - CHECK_THROWS_WITH(json(json::value_t::number_unsigned).get(), + CHECK_THROWS_WITH(json(json::value_t::number_unsigned).get(), "[json.exception.type_error.302] type must be string, but is number"); - CHECK_THROWS_WITH(json(json::value_t::number_float).get(), + CHECK_THROWS_WITH(json(json::value_t::number_float).get(), "[json.exception.type_error.302] type must be string, but is number"); } #endif @@ -562,7 +559,7 @@ TEST_CASE("value conversion") SECTION("std::string_view") { std::string s = "previous value"; - std::string_view sv = s; + string_view sv = s; j.get_to(sv); CHECK(json(sv) == j); } @@ -617,7 +614,7 @@ TEST_CASE("value conversion") #if defined(JSON_HAS_CPP_17) SECTION("std::string_view") { - std::string_view s = j.get(); + string_view s = j.get(); CHECK(json(s) == j); } #endif