Add string_view support
This avoids unnecessary string copies on often used find().
This commit is contained in:
parent
33df3250c3
commit
1a66679929
2 changed files with 40 additions and 9 deletions
34
src/json.hpp
34
src/json.hpp
|
@ -109,6 +109,16 @@ SOFTWARE.
|
||||||
#define JSON_UNLIKELY(x) x
|
#define JSON_UNLIKELY(x) x
|
||||||
#endif
|
#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
|
@brief namespace for Niels Lohmann
|
||||||
@see https://github.com/nlohmann
|
@see https://github.com/nlohmann
|
||||||
|
@ -7589,12 +7599,28 @@ class basic_json
|
||||||
7159](http://rfc7159.net/rfc7159), because any order implements the
|
7159](http://rfc7159.net/rfc7159), because any order implements the
|
||||||
specified "unordered" nature of JSON objects.
|
specified "unordered" nature of JSON objects.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#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,
|
using object_t = ObjectType<StringType,
|
||||||
basic_json,
|
basic_json,
|
||||||
std::less<StringType>,
|
std::less<StringType>,
|
||||||
AllocatorType<std::pair<const StringType,
|
AllocatorType<std::pair<const StringType,
|
||||||
basic_json>>>;
|
basic_json>>>;
|
||||||
|
|
||||||
|
using object_comparator_key_t = typename object_t::key_type;
|
||||||
|
#endif
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief a type for an array
|
@brief a type for an array
|
||||||
|
|
||||||
|
@ -10880,7 +10906,7 @@ class basic_json
|
||||||
|
|
||||||
@since version 1.0.0
|
@since version 1.0.0
|
||||||
*/
|
*/
|
||||||
iterator find(typename object_t::key_type key)
|
iterator find(object_comparator_key_t key)
|
||||||
{
|
{
|
||||||
auto result = end();
|
auto result = end();
|
||||||
|
|
||||||
|
@ -10894,9 +10920,9 @@ class basic_json
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief find an element in a JSON object
|
@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();
|
auto result = cend();
|
||||||
|
|
||||||
|
@ -10929,7 +10955,7 @@ class basic_json
|
||||||
|
|
||||||
@since version 1.0.0
|
@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 0 for all nonobject types
|
||||||
return is_object() ? m_value.object->count(key) : 0;
|
return is_object() ? m_value.object->count(key) : 0;
|
||||||
|
|
|
@ -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 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
|
# 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")
|
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()
|
endif()
|
||||||
|
|
||||||
#############################################################################
|
#############################################################################
|
||||||
|
|
Loading…
Reference in a new issue