Allow to use string_view from std::experimental

This commit is contained in:
Alexander Karzhenkov 2020-08-21 10:05:51 +05:00
parent 1943399433
commit 5eddcc134d
4 changed files with 58 additions and 23 deletions

View File

@ -31,6 +31,25 @@
#define JSON_HAS_CPP_14 #define JSON_HAS_CPP_14
#endif #endif
namespace nlohmann {
namespace std_aliases { }
using namespace std_aliases;
}
#if defined(JSON_HAS_CPP_17)
#if __has_include(<string_view>)
#include <string_view>
namespace nlohmann::std_aliases {
using std::string_view;
}
#elif __has_include(<experimental/string_view>)
#include <experimental/string_view>
namespace nlohmann::std_aliases {
using std::experimental::string_view;
}
#endif
#endif
// disable float-equal warnings on GCC/clang // disable float-equal warnings on GCC/clang
#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
#pragma GCC diagnostic push #pragma GCC diagnostic push

View File

@ -3230,7 +3230,7 @@ class basic_json
!detail::is_basic_json<ValueType>::value !detail::is_basic_json<ValueType>::value
&& !std::is_same<ValueType, std::initializer_list<typename string_t::value_type>>::value && !std::is_same<ValueType, std::initializer_list<typename string_t::value_type>>::value
#if defined(JSON_HAS_CPP_17) && (defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER <= 1914)) #if defined(JSON_HAS_CPP_17) && (defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER <= 1914))
&& !std::is_same<ValueType, typename std::string_view>::value && !std::is_same<ValueType, string_view>::value
#endif #endif
&& detail::is_detected<detail::get_template_function, const basic_json_t&, ValueType>::value && detail::is_detected<detail::get_template_function, const basic_json_t&, ValueType>::value
, int >::type = 0 > , int >::type = 0 >

View File

@ -2048,6 +2048,25 @@ JSON_HEDLEY_DIAGNOSTIC_POP
#define JSON_HAS_CPP_14 #define JSON_HAS_CPP_14
#endif #endif
namespace nlohmann {
namespace std_aliases { }
using namespace std_aliases;
}
#if defined(JSON_HAS_CPP_17)
#if __has_include(<string_view>)
#include <string_view>
namespace nlohmann::std_aliases {
using std::string_view;
}
#elif __has_include(<experimental/string_view>)
#include <experimental/string_view>
namespace nlohmann::std_aliases {
using std::experimental::string_view;
}
#endif
#endif
// disable float-equal warnings on GCC/clang // disable float-equal warnings on GCC/clang
#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
#pragma GCC diagnostic push #pragma GCC diagnostic push
@ -19736,7 +19755,7 @@ class basic_json
!detail::is_basic_json<ValueType>::value !detail::is_basic_json<ValueType>::value
&& !std::is_same<ValueType, std::initializer_list<typename string_t::value_type>>::value && !std::is_same<ValueType, std::initializer_list<typename string_t::value_type>>::value
#if defined(JSON_HAS_CPP_17) && (defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER <= 1914)) #if defined(JSON_HAS_CPP_17) && (defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER <= 1914))
&& !std::is_same<ValueType, typename std::string_view>::value && !std::is_same<ValueType, string_view>::value
#endif #endif
&& detail::is_detected<detail::get_template_function, const basic_json_t&, ValueType>::value && detail::is_detected<detail::get_template_function, const basic_json_t&, ValueType>::value
, int >::type = 0 > , int >::type = 0 >

View File

@ -32,6 +32,7 @@ SOFTWARE.
#define JSON_TESTS_PRIVATE #define JSON_TESTS_PRIVATE
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using nlohmann::json; using nlohmann::json;
using namespace nlohmann::std_aliases;
#include <deque> #include <deque>
#include <forward_list> #include <forward_list>
@ -48,10 +49,6 @@ using nlohmann::json;
#define JSON_HAS_CPP_14 #define JSON_HAS_CPP_14
#endif #endif
#if defined(JSON_HAS_CPP_17)
#include <string_view>
#endif
TEST_CASE("value conversion") TEST_CASE("value conversion")
{ {
SECTION("get an object (explicit)") SECTION("get an object (explicit)")
@ -465,7 +462,7 @@ TEST_CASE("value conversion")
#if defined(JSON_HAS_CPP_17) #if defined(JSON_HAS_CPP_17)
SECTION("std::string_view") SECTION("std::string_view")
{ {
std::string_view s = j.get<std::string_view>(); string_view s = j.get<string_view>();
CHECK(json(s) == j); CHECK(json(s) == j);
} }
#endif #endif
@ -514,27 +511,27 @@ TEST_CASE("value conversion")
#if defined(JSON_HAS_CPP_17) #if defined(JSON_HAS_CPP_17)
SECTION("exception in case of a non-string type using string_view") SECTION("exception in case of a non-string type using string_view")
{ {
CHECK_THROWS_AS(json(json::value_t::null).get<std::string_view>(), json::type_error&); CHECK_THROWS_AS(json(json::value_t::null).get<string_view>(), json::type_error&);
CHECK_THROWS_AS(json(json::value_t::object).get<std::string_view>(), json::type_error&); CHECK_THROWS_AS(json(json::value_t::object).get<string_view>(), json::type_error&);
CHECK_THROWS_AS(json(json::value_t::array).get<std::string_view>(), json::type_error&); CHECK_THROWS_AS(json(json::value_t::array).get<string_view>(), json::type_error&);
CHECK_THROWS_AS(json(json::value_t::boolean).get<std::string_view>(), json::type_error&); CHECK_THROWS_AS(json(json::value_t::boolean).get<string_view>(), json::type_error&);
CHECK_THROWS_AS(json(json::value_t::number_integer).get<std::string_view>(), json::type_error&); CHECK_THROWS_AS(json(json::value_t::number_integer).get<string_view>(), json::type_error&);
CHECK_THROWS_AS(json(json::value_t::number_unsigned).get<std::string_view>(), json::type_error&); CHECK_THROWS_AS(json(json::value_t::number_unsigned).get<string_view>(), json::type_error&);
CHECK_THROWS_AS(json(json::value_t::number_float).get<std::string_view>(), json::type_error&); CHECK_THROWS_AS(json(json::value_t::number_float).get<string_view>(), json::type_error&);
CHECK_THROWS_WITH(json(json::value_t::null).get<std::string_view>(), CHECK_THROWS_WITH(json(json::value_t::null).get<string_view>(),
"[json.exception.type_error.302] type must be string, but is null"); "[json.exception.type_error.302] type must be string, but is null");
CHECK_THROWS_WITH(json(json::value_t::object).get<std::string_view>(), CHECK_THROWS_WITH(json(json::value_t::object).get<string_view>(),
"[json.exception.type_error.302] type must be string, but is object"); "[json.exception.type_error.302] type must be string, but is object");
CHECK_THROWS_WITH(json(json::value_t::array).get<std::string_view>(), CHECK_THROWS_WITH(json(json::value_t::array).get<string_view>(),
"[json.exception.type_error.302] type must be string, but is array"); "[json.exception.type_error.302] type must be string, but is array");
CHECK_THROWS_WITH(json(json::value_t::boolean).get<std::string_view>(), CHECK_THROWS_WITH(json(json::value_t::boolean).get<string_view>(),
"[json.exception.type_error.302] type must be string, but is boolean"); "[json.exception.type_error.302] type must be string, but is boolean");
CHECK_THROWS_WITH(json(json::value_t::number_integer).get<std::string_view>(), CHECK_THROWS_WITH(json(json::value_t::number_integer).get<string_view>(),
"[json.exception.type_error.302] type must be string, but is number"); "[json.exception.type_error.302] type must be string, but is number");
CHECK_THROWS_WITH(json(json::value_t::number_unsigned).get<std::string_view>(), CHECK_THROWS_WITH(json(json::value_t::number_unsigned).get<string_view>(),
"[json.exception.type_error.302] type must be string, but is number"); "[json.exception.type_error.302] type must be string, but is number");
CHECK_THROWS_WITH(json(json::value_t::number_float).get<std::string_view>(), CHECK_THROWS_WITH(json(json::value_t::number_float).get<string_view>(),
"[json.exception.type_error.302] type must be string, but is number"); "[json.exception.type_error.302] type must be string, but is number");
} }
#endif #endif
@ -562,7 +559,7 @@ TEST_CASE("value conversion")
SECTION("std::string_view") SECTION("std::string_view")
{ {
std::string s = "previous value"; std::string s = "previous value";
std::string_view sv = s; string_view sv = s;
j.get_to(sv); j.get_to(sv);
CHECK(json(sv) == j); CHECK(json(sv) == j);
} }
@ -617,7 +614,7 @@ TEST_CASE("value conversion")
#if defined(JSON_HAS_CPP_17) #if defined(JSON_HAS_CPP_17)
SECTION("std::string_view") SECTION("std::string_view")
{ {
std::string_view s = j.get<std::string_view>(); string_view s = j.get<string_view>();
CHECK(json(s) == j); CHECK(json(s) == j);
} }
#endif #endif