diff --git a/include/json/config.h b/include/json/config.h index 6426c3b29..b11fdbc39 100644 --- a/include/json/config.h +++ b/include/json/config.h @@ -14,6 +14,11 @@ #include #include +#if __cplusplus >= 201703L +#include +#define JSON_USE_STRING_VIEW 1 +#endif + /// If defined, indicates that json library is embedded in CppTL library. //# define JSON_IN_CPPTL 1 @@ -141,6 +146,9 @@ using Allocator = typename std::conditional, std::allocator>::type; using String = std::basic_string, Allocator>; +#ifdef JSON_USE_STRING_VIEW +using StringView = std::basic_string_view>; +#endif using IStringStream = std::basic_istringstream; diff --git a/include/json/value.h b/include/json/value.h index a47884854..41d09b564 100644 --- a/include/json/value.h +++ b/include/json/value.h @@ -325,6 +325,9 @@ class JSON_API Value { */ Value(const StaticString& value); Value(const String& value); +#ifdef JSON_USE_STRING_VIEW + Value(const StringView value); +#endif #ifdef JSON_USE_CPPTL Value(const CppTL::ConstString& value); #endif @@ -464,6 +467,12 @@ class JSON_API Value { /// that name. /// \param key may contain embedded nulls. const Value& operator[](const String& key) const; + +#ifdef JSON_USE_STRING_VIEW + Value& operator[](const StringView key); + const Value& operator[](const StringView key) const; +#endif + /** \brief Access an object value by name, create a null member if it does not * exist. * diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index 6024212f2..9e539e8e8 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -438,6 +438,14 @@ Value::Value(const String& value) { value.data(), static_cast(value.length())); } +#ifdef JSON_USE_STRING_VIEW +Value::Value(const StringView value) { + initBasic(stringValue, true); + value_.string_ = duplicateAndPrefixStringValue( + value.data(), static_cast(value.length())); +} +#endif + Value::Value(const StaticString& value) { initBasic(stringValue); value_.string_ = const_cast(value.c_str()); @@ -1149,6 +1157,14 @@ Value const& Value::operator[](const String& key) const { return nullSingleton(); return *found; } +#ifdef JSON_USE_STRING_VIEW +Value const& Value::operator[](const StringView key) const { + Value const* found = find(sv.data(), sv.data() + sv.length()); + if (!found) + return nullSingleton(); + return *found; +} +#endif Value& Value::operator[](const char* key) { return resolveReference(key, key + strlen(key)); @@ -1158,6 +1174,12 @@ Value& Value::operator[](const String& key) { return resolveReference(key.data(), key.data() + key.length()); } +#ifdef JSON_USE_STRING_VIEW +Value& Value::operator[](const StringView key) { + return resolveReference(key.data(), key.data() + key.length()); +} +#endif + Value& Value::operator[](const StaticString& key) { return resolveReference(key.c_str()); } diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index e700c3801..eb74b17b8 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -300,6 +300,18 @@ JSONTEST_FIXTURE(ValueTest, arrays) { JSONTEST_ASSERT_EQUAL(Json::Value(17), got); JSONTEST_ASSERT_EQUAL(false, array1_.removeIndex(2, &got)); // gone now } + +#ifdef JSON_USE_STRING_VIEW +JSONTEST_FIXTURE(ValueTest, stringView) { + Json::Value root; + Json::Value item("Test item"); + std::string_view sv = "array"; + std::string s = "array"; + root[s] = item; + JSONTEST_ASSERT_EQUAL(item.asString(), root[sv].asString()); +} +#endif + JSONTEST_FIXTURE(ValueTest, arrayIssue252) { int count = 5; Json::Value root;