Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions include/json/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <ostream>
#include <sstream>
#include <string>
#include <string_view>
Copy link
Member

Choose a reason for hiding this comment

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

It seems the system doesn't support C++17 Standard now, so it cannot open include file string_view
but if you like, you could use the __plusplus marco switch :

#if __cplusplus >= 201703L 
#include <string_view>
#endif 

#include <type_traits>

/// If defined, indicates that json library is embedded in CppTL library.
Expand Down Expand Up @@ -141,6 +142,7 @@ 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>>;
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.

using IStringStream = std::basic_istringstream<String::value_type,
String::traits_type,
String::allocator_type>;
Expand Down
3 changes: 3 additions & 0 deletions include/json/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ class JSON_API Value {
*/
Value(const StaticString& value);
Value(const String& value);
Value(const StringView value);
#ifdef JSON_USE_CPPTL
Value(const CppTL::ConstString& value);
#endif
Expand Down Expand Up @@ -464,6 +465,8 @@ class JSON_API Value {
/// that name.
/// \param key may contain embedded nulls.
const Value& operator[](const String& key) const;
Value& operator[](const StringView key);
const Value& operator[](const StringView key) const;
/** \brief Access an object value by name, create a null member if it does not
* exist.
*
Expand Down
16 changes: 16 additions & 0 deletions src/lib_json/json_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,12 @@ Value::Value(const String& value) {
value.data(), static_cast<unsigned>(value.length()));
}

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

Value::Value(const StaticString& value) {
initBasic(stringValue);
value_.string_ = const_cast<char*>(value.c_str());
Expand Down Expand Up @@ -1149,6 +1155,12 @@ Value const& Value::operator[](const String& key) const {
return nullSingleton();
return *found;
}
Value const& Value::operator[](const StringView key) const {
Value const* found = find(sv.data(), sv.data() + sv.length());
if (!found)
return nullSingleton();
return *found;
}

Value& Value::operator[](const char* key) {
return resolveReference(key, key + strlen(key));
Expand All @@ -1158,6 +1170,10 @@ Value& Value::operator[](const String& key) {
return resolveReference(key.data(), key.data() + key.length());
}

Value& Value::operator[](const StringView key) {
return resolveReference(key.data(), key.data() + key.length());
}

Value& Value::operator[](const StaticString& key) {
return resolveReference(key.c_str());
}
Expand Down