Skip to content

feat: support std::string_view in Value API #1584

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

Merged
merged 2 commits into from
Jan 10, 2025
Merged
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
61 changes: 54 additions & 7 deletions include/json/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,21 @@
#endif
#endif

#if __cplusplus >= 201703L
#define JSONCPP_HAS_STRING_VIEW 1
#endif

#include <array>
#include <exception>
#include <map>
#include <memory>
#include <string>
#include <vector>

#ifdef JSONCPP_HAS_STRING_VIEW
#include <string_view>
#endif

// Disable warning C4251: <data member>: <type> needs to have dll-interface to
// be used by...
#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Expand Down Expand Up @@ -342,6 +350,9 @@ class JSON_API Value {
*/
Value(const StaticString& value);
Value(const String& value);
#ifdef JSONCPP_HAS_STRING_VIEW
Value(std::string_view value);
#endif
Value(bool value);
Value(std::nullptr_t ptr) = delete;
Value(const Value& other);
Expand Down Expand Up @@ -384,6 +395,12 @@ class JSON_API Value {
* \return false if !string. (Seg-fault if str or end are NULL.)
*/
bool getString(char const** begin, char const** end) const;
#ifdef JSONCPP_HAS_STRING_VIEW
/** Get string_view of string-value.
* \return false if !string. (Seg-fault if str is NULL.)
*/
bool getString(std::string_view* str) const;
#endif
Int asInt() const;
UInt asUInt() const;
#if defined(JSON_HAS_INT64)
Expand Down Expand Up @@ -470,6 +487,15 @@ class JSON_API Value {
bool insert(ArrayIndex index, const Value& newValue);
bool insert(ArrayIndex index, Value&& newValue);

#ifdef JSONCPP_HAS_STRING_VIEW
/// Access an object value by name, create a null member if it does not exist.
/// \param key may contain embedded nulls.
Value& operator[](std::string_view key);
/// Access an object value by name, returns null if there is no member with
/// that name.
/// \param key may contain embedded nulls.
const Value& operator[](std::string_view key) const;
#else
/// Access an object value by name, create a null member if it does not exist.
/// \note Because of our implementation, keys are limited to 2^30 -1 chars.
/// Exceeding that will cause an exception.
Expand All @@ -484,6 +510,7 @@ class JSON_API Value {
/// that name.
/// \param key may contain embedded nulls.
const Value& operator[](const String& key) const;
#endif
/** \brief Access an object value by name, create a null member if it does not
* exist.
*
Expand All @@ -497,18 +524,24 @@ class JSON_API Value {
* \endcode
*/
Value& operator[](const StaticString& key);
#ifdef JSONCPP_HAS_STRING_VIEW
/// Return the member named key if it exist, defaultValue otherwise.
/// \note deep copy
Value get(const char* key, const Value& defaultValue) const;
Value get(std::string_view key, const Value& defaultValue) const;
#else
/// Return the member named key if it exist, defaultValue otherwise.
/// \note deep copy
/// \note key may contain embedded nulls.
Value get(const char* begin, const char* end,
const Value& defaultValue) const;
Value get(const char* key, const Value& defaultValue) const;
/// Return the member named key if it exist, defaultValue otherwise.
/// \note deep copy
/// \param key may contain embedded nulls.
Value get(const String& key, const Value& defaultValue) const;
#endif
/// Return the member named key if it exist, defaultValue otherwise.
/// \note deep copy
/// \note key may contain embedded nulls.
Value get(const char* begin, const char* end,
const Value& defaultValue) const;
/// Most general and efficient version of isMember()const, get()const,
/// and operator[]const
/// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
Expand All @@ -525,20 +558,28 @@ class JSON_API Value {
/// Do nothing if it did not exist.
/// \pre type() is objectValue or nullValue
/// \post type() is unchanged
#if JSONCPP_HAS_STRING_VIEW
void removeMember(std::string_view key);
#else
void removeMember(const char* key);
/// Same as removeMember(const char*)
/// \param key may contain embedded nulls.
void removeMember(const String& key);
/// Same as removeMember(const char* begin, const char* end, Value* removed),
/// but 'key' is null-terminated.
bool removeMember(const char* key, Value* removed);
#endif
/** \brief Remove the named map member.
*
* Update 'removed' iff removed.
* \param key may contain embedded nulls.
* \return true iff removed (no exceptions)
*/
#if JSONCPP_HAS_STRING_VIEW
bool removeMember(std::string_view key, Value* removed);
#else
bool removeMember(String const& key, Value* removed);
/// Same as removeMember(const char* begin, const char* end, Value* removed),
/// but 'key' is null-terminated.
bool removeMember(const char* key, Value* removed);
#endif
/// Same as removeMember(String const& key, Value* removed)
bool removeMember(const char* begin, const char* end, Value* removed);
/** \brief Remove the indexed array element.
Expand All @@ -549,12 +590,18 @@ class JSON_API Value {
*/
bool removeIndex(ArrayIndex index, Value* removed);

#ifdef JSONCPP_HAS_STRING_VIEW
/// Return true if the object has a member named key.
/// \param key may contain embedded nulls.
bool isMember(std::string_view key) const;
#else
/// Return true if the object has a member named key.
/// \note 'key' must be null-terminated.
bool isMember(const char* key) const;
/// Return true if the object has a member named key.
/// \param key may contain embedded nulls.
bool isMember(const String& key) const;
#endif
/// Same as isMember(String const& key)const
bool isMember(const char* begin, const char* end) const;

Expand Down
71 changes: 71 additions & 0 deletions src/lib_json/json_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
#include <sstream>
#include <utility>

#ifdef JSONCPP_HAS_STRING_VIEW
#include <string_view>
#endif

// Provide implementation equivalent of std::snprintf for older _MSC compilers
#if defined(_MSC_VER) && _MSC_VER < 1900
#include <stdarg.h>
Expand Down Expand Up @@ -420,6 +424,14 @@ Value::Value(const String& value) {
value.data(), static_cast<unsigned>(value.length()));
}

#ifdef JSONCPP_HAS_STRING_VIEW
Value::Value(std::string_view 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 @@ -627,6 +639,21 @@ bool Value::getString(char const** begin, char const** end) const {
return true;
}

#ifdef JSONCPP_HAS_STRING_VIEW
bool Value::getString(std::string_view* str) const {
if (type() != stringValue)
return false;
if (value_.string_ == nullptr)
return false;
const char* begin;
unsigned length;
decodePrefixedString(this->isAllocated(), this->value_.string_, &length,
&begin);
*str = std::string_view(begin, length);
return true;
}
#endif

String Value::asString() const {
switch (type()) {
case nullValue:
Expand Down Expand Up @@ -1108,6 +1135,17 @@ Value* Value::demand(char const* begin, char const* end) {
"objectValue or nullValue");
return &resolveReference(begin, end);
}
#ifdef JSONCPP_HAS_STRING_VIEW
const Value& Value::operator[](std::string_view key) const {
Value const* found = find(key.data(), key.data() + key.length());
if (!found)
return nullSingleton();
return *found;
}
Value& Value::operator[](std::string_view key) {
return resolveReference(key.data(), key.data() + key.length());
}
#else
const Value& Value::operator[](const char* key) const {
Value const* found = find(key, key + strlen(key));
if (!found)
Expand All @@ -1128,6 +1166,7 @@ Value& Value::operator[](const char* key) {
Value& Value::operator[](const String& key) {
return resolveReference(key.data(), key.data() + key.length());
}
#endif

Value& Value::operator[](const StaticString& key) {
return resolveReference(key.c_str());
Expand Down Expand Up @@ -1167,12 +1206,18 @@ Value Value::get(char const* begin, char const* end,
Value const* found = find(begin, end);
return !found ? defaultValue : *found;
}
#ifdef JSONCPP_HAS_STRING_VIEW
Value Value::get(std::string_view key, const Value& defaultValue) const {
return get(key.data(), key.data() + key.length(), defaultValue);
}
#else
Value Value::get(char const* key, Value const& defaultValue) const {
return get(key, key + strlen(key), defaultValue);
}
Value Value::get(String const& key, Value const& defaultValue) const {
return get(key.data(), key.data() + key.length(), defaultValue);
}
#endif

bool Value::removeMember(const char* begin, const char* end, Value* removed) {
if (type() != objectValue) {
Expand All @@ -1188,12 +1233,31 @@ bool Value::removeMember(const char* begin, const char* end, Value* removed) {
value_.map_->erase(it);
return true;
}
#ifdef JSONCPP_HAS_STRING_VIEW
bool Value::removeMember(std::string_view key, Value* removed) {
return removeMember(key.data(), key.data() + key.length(), removed);
}
#else
bool Value::removeMember(const char* key, Value* removed) {
return removeMember(key, key + strlen(key), removed);
}
bool Value::removeMember(String const& key, Value* removed) {
return removeMember(key.data(), key.data() + key.length(), removed);
}
#endif

#ifdef JSONCPP_HAS_STRING_VIEW
void Value::removeMember(std::string_view key) {
JSON_ASSERT_MESSAGE(type() == nullValue || type() == objectValue,
"in Json::Value::removeMember(): requires objectValue");
if (type() == nullValue)
return;

CZString actualKey(key.data(), unsigned(key.length()),
CZString::noDuplication);
value_.map_->erase(actualKey);
}
#else
void Value::removeMember(const char* key) {
JSON_ASSERT_MESSAGE(type() == nullValue || type() == objectValue,
"in Json::Value::removeMember(): requires objectValue");
Expand All @@ -1204,6 +1268,7 @@ void Value::removeMember(const char* key) {
value_.map_->erase(actualKey);
}
void Value::removeMember(const String& key) { removeMember(key.c_str()); }
#endif

bool Value::removeIndex(ArrayIndex index, Value* removed) {
if (type() != arrayValue) {
Expand Down Expand Up @@ -1233,12 +1298,18 @@ bool Value::isMember(char const* begin, char const* end) const {
Value const* value = find(begin, end);
return nullptr != value;
}
#ifdef JSONCPP_HAS_STRING_VIEW
bool Value::isMember(std::string_view key) const {
return isMember(key.data(), key.data() + key.length());
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this works since it's behind a define and conditionally enabled. Ideally we would be able to have the string_view isMember contain the actual logic and use modern C++ in the find implementation.

}
#else
bool Value::isMember(char const* key) const {
return isMember(key, key + strlen(key));
}
bool Value::isMember(String const& key) const {
return isMember(key.data(), key.data() + key.length());
}
#endif

Value::Members Value::getMemberNames() const {
JSON_ASSERT_MESSAGE(
Expand Down
Loading