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
1a666799
Commit
1a666799
authored
Oct 21, 2017
by
Jamie Seward
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add string_view support
This avoids unnecessary string copies on often used find().
parent
33df3250
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
9 deletions
+40
-9
src/json.hpp
src/json.hpp
+35
-9
test/CMakeLists.txt
test/CMakeLists.txt
+5
-0
No files found.
src/json.hpp
View file @
1a666799
...
...
@@ -109,6 +109,16 @@ SOFTWARE.
#define JSON_UNLIKELY(x) x
#endif
// string_view support
#if defined(_MSC_VER) && defined(_HAS_CXX17)
#define JSON_USE_STRING_VIEW
#endif
#if defined(JSON_USE_STRING_VIEW)
#include <string_view>
#endif
/*!
@brief namespace for Niels Lohmann
@see https://github.com/nlohmann
...
...
@@ -7589,11 +7599,27 @@ class basic_json
7159](http://rfc7159.net/rfc7159), because any order implements the
specified "unordered" nature of JSON objects.
*/
using
object_t
=
ObjectType
<
StringType
,
basic_json
,
std
::
less
<
StringType
>
,
AllocatorType
<
std
::
pair
<
const
StringType
,
basic_json
>>>
;
#if defined(JSON_USE_STRING_VIEW)
// if std::string_view is to be used the object_t must
// be declared with a transparent comparator
// https://stackoverflow.com/questions/35525777/use-of-string-view-for-map-lookup
using
object_t
=
ObjectType
<
StringType
,
basic_json
,
std
::
less
<>
,
AllocatorType
<
std
::
pair
<
const
StringType
,
basic_json
>>>
;
using
object_comparator_key_t
=
std
::
string_view
;
#else
using
object_t
=
ObjectType
<
StringType
,
basic_json
,
std
::
less
<
StringType
>
,
AllocatorType
<
std
::
pair
<
const
StringType
,
basic_json
>>>
;
using
object_comparator_key_t
=
typename
object_t
::
key_type
;
#endif
/*!
@brief a type for an array
...
...
@@ -10880,7 +10906,7 @@ class basic_json
@since version 1.0.0
*/
iterator
find
(
typename
object_t
::
key_type
key
)
iterator
find
(
object_comparator_key_t
key
)
{
auto
result
=
end
();
...
...
@@ -10894,9 +10920,9 @@ class basic_json
/*!
@brief find an element in a JSON object
@copydoc find(
typename object_t::key_type
)
@copydoc find(
object_transparent_comparator_key_t
)
*/
const_iterator
find
(
typename
object_t
::
key_type
key
)
const
const_iterator
find
(
object_comparator_key_t
key
)
const
{
auto
result
=
cend
();
...
...
@@ -10929,7 +10955,7 @@ class basic_json
@since version 1.0.0
*/
size_type
count
(
typename
object_t
::
key_type
key
)
const
size_type
count
(
object_comparator_key_t
key
)
const
{
// return 0 for all nonobject types
return
is_object
()
?
m_value
.
object
->
count
(
key
)
:
0
;
...
...
test/CMakeLists.txt
View file @
1a666799
...
...
@@ -80,6 +80,11 @@ if(MSVC)
# Disable warning C4566: character represented by universal-character-name '\uFF01' cannot be represented in the current code page (1252)
# Disable warning C4996: 'nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>::operator <<': was declared deprecated
set
(
CMAKE_CXX_FLAGS
"
${
CMAKE_CXX_FLAGS
}
/wd4389 /wd4309 /wd4566 /wd4996"
)
if
(
MSVC_VERSION GREATER_EQUAL 1910
)
# Enable c++17 support in Visual Studio 2017 (for testing string_view support)
set
(
CMAKE_CXX_FLAGS
"
${
CMAKE_CXX_FLAGS
}
/std:c++17"
)
endif
()
endif
()
#############################################################################
...
...
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