Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
json
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
json
Commits
bf348ca8
Unverified
Commit
bf348ca8
authored
Jun 23, 2018
by
Niels Lohmann
Committed by
GitHub
Jun 23, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1028 from gracicot/develop
Added support for string_view in C++17
parents
ed6a0686
14e6278c
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
152 additions
and
4 deletions
+152
-4
include/nlohmann/detail/conversions/from_json.hpp
include/nlohmann/detail/conversions/from_json.hpp
+17
-0
include/nlohmann/detail/conversions/to_json.hpp
include/nlohmann/detail/conversions/to_json.hpp
+10
-0
include/nlohmann/detail/meta.hpp
include/nlohmann/detail/meta.hpp
+20
-0
include/nlohmann/json.hpp
include/nlohmann/json.hpp
+2
-2
single_include/nlohmann/json.hpp
single_include/nlohmann/json.hpp
+49
-2
test/src/unit-conversions.cpp
test/src/unit-conversions.cpp
+54
-0
No files found.
include/nlohmann/detail/conversions/from_json.hpp
View file @
bf348ca8
...
...
@@ -72,6 +72,23 @@ 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
,
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
())));
}
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
)
{
...
...
include/nlohmann/detail/conversions/to_json.hpp
View file @
bf348ca8
...
...
@@ -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
>
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
<
>
...
...
include/nlohmann/detail/meta.hpp
View file @
bf348ca8
...
...
@@ -120,6 +120,17 @@ 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
and
std
::
is_constructible
<
RealType
,
CompatibleStringType
>::
value
;
};
template
<
class
BasicJsonType
,
class
CompatibleObjectType
>
struct
is_compatible_object_type
{
...
...
@@ -130,6 +141,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
{
...
...
include/nlohmann/json.hpp
View file @
bf348ca8
...
...
@@ -2829,9 +2829,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
...
...
single_include/nlohmann/json.hpp
View file @
bf348ca8
...
...
@@ -354,6 +354,17 @@ 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
and
std
::
is_constructible
<
RealType
,
CompatibleStringType
>::
value
;
};
template
<
class
BasicJsonType
,
class
CompatibleObjectType
>
struct
is_compatible_object_type
{
...
...
@@ -364,6 +375,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
{
...
...
@@ -980,6 +1000,23 @@ 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
,
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
())));
}
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
)
{
...
...
@@ -1324,6 +1361,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
<
>
...
...
@@ -13588,9 +13635,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
...
...
test/src/unit-conversions.cpp
View file @
bf348ca8
...
...
@@ -40,6 +40,17 @@ using nlohmann::json;
#include <unordered_set>
#include <valarray>
#if (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464
#define JSON_HAS_CPP_17
#define JSON_HAS_CPP_14
#elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1)
#define JSON_HAS_CPP_14
#endif
#if defined(JSON_HAS_CPP_17)
#include <string_view>
#endif
TEST_CASE
(
"value conversion"
)
{
SECTION
(
"get an object (explicit)"
)
...
...
@@ -344,6 +355,13 @@ TEST_CASE("value conversion")
std
::
string
s
=
j
.
get
<
std
::
string
>
();
CHECK
(
json
(
s
)
==
j
);
}
#if defined(JSON_HAS_CPP_17)
SECTION
(
"std::string_view"
)
{
std
::
string_view
s
=
j
.
get
<
std
::
string_view
>
();
CHECK
(
json
(
s
)
==
j
);
}
#endif
SECTION
(
"exception in case of a non-string type"
)
{
...
...
@@ -385,6 +403,34 @@ TEST_CASE("value conversion")
json
(
json
::
value_t
::
number_float
).
get
<
json
::
string_t
>
(),
"[json.exception.type_error.302] type must be string, but is number"
);
}
#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
<
std
::
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
::
array
).
get
<
std
::
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
::
number_integer
).
get
<
std
::
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_float
).
get
<
std
::
string_view
>
(),
json
::
type_error
&
);
CHECK_THROWS_WITH
(
json
(
json
::
value_t
::
null
).
get
<
std
::
string_view
>
(),
"[json.exception.type_error.302] type must be string, but is null"
);
CHECK_THROWS_WITH
(
json
(
json
::
value_t
::
object
).
get
<
std
::
string_view
>
(),
"[json.exception.type_error.302] type must be string, but is object"
);
CHECK_THROWS_WITH
(
json
(
json
::
value_t
::
array
).
get
<
std
::
string_view
>
(),
"[json.exception.type_error.302] type must be string, but is array"
);
CHECK_THROWS_WITH
(
json
(
json
::
value_t
::
boolean
).
get
<
std
::
string_view
>
(),
"[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
>
(),
"[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
>
(),
"[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
>
(),
"[json.exception.type_error.302] type must be string, but is number"
);
}
#endif
}
SECTION
(
"get a string (implicit)"
)
...
...
@@ -398,6 +444,14 @@ TEST_CASE("value conversion")
CHECK
(
json
(
s
)
==
j
);
}
#if defined(JSON_HAS_CPP_17)
SECTION
(
"std::string_view"
)
{
std
::
string_view
s
=
j
;
CHECK
(
json
(
s
)
==
j
);
}
#endif
SECTION
(
"std::string"
)
{
std
::
string
s
=
j
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment