Skip to content

Add cxx17 string_view operator[] for Json::Value #999

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/json/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
#include <string>
#include <type_traits>

#if __cplusplus >= 201703L
#include <string_view>
#define JSON_USE_STRING_VIEW 1
#endif

/// If defined, indicates that json library is embedded in CppTL library.
//# define JSON_IN_CPPTL 1

Expand Down Expand Up @@ -141,6 +146,9 @@ using Allocator = typename std::conditional<JSONCPP_USING_SECURE_MEMORY,
SecureAllocator<T>,
std::allocator<T>>::type;
using String = std::basic_string<char, std::char_traits<char>, Allocator<char>>;
#ifdef JSON_USE_STRING_VIEW
using StringView = std::basic_string_view<char, std::char_traits<char>>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only when the __cplusplus >= 201703L, can this semantics work.

#endif
using IStringStream = std::basic_istringstream<String::value_type,
String::traits_type,
String::allocator_type>;
Expand Down
9 changes: 9 additions & 0 deletions include/json/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*
Expand Down
22 changes: 22 additions & 0 deletions src/lib_json/json_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,14 @@ Value::Value(const String& value) {
value.data(), static_cast<unsigned>(value.length()));
}

#ifdef JSON_USE_STRING_VIEW
Value::Value(const StringView value) {
initBasic(stringValue, true);
value_.string_ = duplicateAndPrefixStringValue(
value.data(), static_cast<unsigned>(value.length()));
}
#endif

Value::Value(const StaticString& value) {
initBasic(stringValue);
value_.string_ = const_cast<char*>(value.c_str());
Expand Down Expand Up @@ -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));
Expand All @@ -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());
}
Expand Down
12 changes: 12 additions & 0 deletions src/test_lib_json/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down