-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support for string_view in C++17 #1028
Changes from 8 commits
8165707
2a5506e
53d8d57
c78dbc3
aaee18c
714c592
4778c02
e0e7fa3
5676a2a
7acd90b
14e6278
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,16 @@ struct external_constructor<value_t::string> | |
j.m_value = std::move(s); | ||
j.assert_invariant(); | ||
} | ||
|
||
template<typename BasicJsonType, typename CompatibleStringType, | ||
enable_if_t<not std::is_same<CompatibleStringType, typename BasicJsonType::string_t>::value, | ||
int> = 0> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This SFINAE check is already done in the
Additionally, I'd rather keep This is mostly a style preference though. |
||
static void construct(BasicJsonType& j, const CompatibleStringType& str) | ||
{ | ||
j.m_type = value_t::string; | ||
j.m_value.string = j.template create<typename BasicJsonType::string_t>(str); | ||
j.assert_invariant(); | ||
} | ||
}; | ||
|
||
template<> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -353,6 +353,16 @@ struct is_compatible_object_type_impl<true, RealType, CompatibleObjectType> | |
std::is_constructible<typename RealType::mapped_type, typename CompatibleObjectType::mapped_type>::value; | ||
}; | ||
|
||
template<bool B, class RealType, class CompatibleStringType> | ||
struct is_compatible_string_type_impl : std::false_type {}; | ||
|
||
template<class RealType, class CompatibleStringType> | ||
struct is_compatible_string_type_impl<true, RealType, CompatibleStringType> | ||
{ | ||
static constexpr auto value = | ||
std::is_same<typename RealType::value_type, typename CompatibleStringType::value_type>::value; | ||
}; | ||
|
||
template<class BasicJsonType, class CompatibleObjectType> | ||
struct is_compatible_object_type | ||
{ | ||
|
@@ -363,6 +373,15 @@ struct is_compatible_object_type | |
typename BasicJsonType::object_t, CompatibleObjectType >::value; | ||
}; | ||
|
||
template<class BasicJsonType, class CompatibleStringType> | ||
struct is_compatible_string_type | ||
{ | ||
static auto constexpr value = is_compatible_string_type_impl < | ||
conjunction<negation<std::is_same<void, CompatibleStringType>>, | ||
has_value_type<CompatibleStringType>>::value, | ||
typename BasicJsonType::string_t, CompatibleStringType >::value; | ||
}; | ||
|
||
template<typename BasicJsonType, typename T> | ||
struct is_basic_json_nested_type | ||
{ | ||
|
@@ -977,6 +996,25 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s) | |
s = *j.template get_ptr<const typename BasicJsonType::string_t*>(); | ||
} | ||
|
||
template < | ||
typename BasicJsonType, typename CompatibleStringType, | ||
enable_if_t < | ||
is_compatible_string_type<BasicJsonType, CompatibleStringType>::value and | ||
not std::is_same<typename BasicJsonType::string_t, | ||
CompatibleStringType>::value and | ||
std::is_constructible < | ||
BasicJsonType, typename CompatibleStringType::value_type >::value, | ||
int > = 0 > | ||
void from_json(const BasicJsonType& j, CompatibleStringType& s) | ||
{ | ||
if (JSON_UNLIKELY(not j.is_string())) | ||
{ | ||
JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a test case checking for this exception? If not, please add one. |
||
} | ||
|
||
s = *j.template get_ptr<const typename BasicJsonType::string_t*>(); | ||
} | ||
|
||
template<typename BasicJsonType> | ||
void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val) | ||
{ | ||
|
@@ -1283,6 +1321,16 @@ struct external_constructor<value_t::string> | |
j.m_value = std::move(s); | ||
j.assert_invariant(); | ||
} | ||
|
||
template<typename BasicJsonType, typename CompatibleStringType, | ||
enable_if_t<not std::is_same<CompatibleStringType, typename BasicJsonType::string_t>::value, | ||
int> = 0> | ||
static void construct(BasicJsonType& j, const CompatibleStringType& str) | ||
{ | ||
j.m_type = value_t::string; | ||
j.m_value.string = j.template create<typename BasicJsonType::string_t>(str); | ||
j.assert_invariant(); | ||
} | ||
}; | ||
|
||
template<> | ||
|
@@ -12432,9 +12480,9 @@ class basic_json | |
not detail::is_basic_json<ValueType>::value | ||
#ifndef _MSC_VER // fix for issue #167 operator<< ambiguity under VS2015 | ||
and not std::is_same<ValueType, std::initializer_list<typename string_t::value_type>>::value | ||
#endif | ||
#if defined(JSON_HAS_CPP_17) | ||
#if defined(JSON_HAS_CPP_17) && _MSC_VER <= 1914 | ||
and not std::is_same<ValueType, typename std::string_view>::value | ||
#endif | ||
#endif | ||
, int >::type = 0 > | ||
operator ValueType() const | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this check relevant?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right, it's not necessary. I also missed a check elsewhere, let me fix that.