-
-
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8165707
basic_json now supports getting many type of strings
gracicot 2a5506e
Amalgamated headers
gracicot 53d8d57
Amalgamate single include
gracicot c78dbc3
Added test for conversion to string_view
gracicot aaee18c
Added test for string conversion with string_view
gracicot 714c592
Disabled implicit conversion to string_view on MSVC 15.13 and older
gracicot 4778c02
Set MSVC version from 1514 and older
gracicot e0e7fa3
Re-added external_constructor with string compatible types
gracicot 5676a2a
Aligned template declaration
gracicot 7acd90b
Fixed check for compatible string type
gracicot 14e6278
Merge branch 'develop' of github.com:gracicot/json into develop
gracicot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.