Skip to content

Commit

Permalink
Added Value::find with String key (#1467)
Browse files Browse the repository at this point in the history
* Added Value::find with String key

* Fix codestyle

---------

Co-authored-by: Jordan Bayles <bayles.jordan@gmail.com>
Co-authored-by: Petukhov Timofey <Timofey.Petukhov@infotecs.ru>
  • Loading branch information
3 people authored Sep 10, 2024
1 parent 2067f66 commit 7f36cdb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
3 changes: 3 additions & 0 deletions include/json/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,9 @@ class JSON_API Value {
/// and operator[]const
/// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
Value const* find(char const* begin, char const* end) const;
/// Most general and efficient version of isMember()const, get()const,
/// and operator[]const
Value const* find(const String& key) const;
/// Most general and efficient version of object-mutators.
/// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
/// \return non-zero, but JSON_ASSERT if this is neither object nor nullValue.
Expand Down
5 changes: 4 additions & 1 deletion src/lib_json/json_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,9 @@ Value const* Value::find(char const* begin, char const* end) const {
return nullptr;
return &(*it).second;
}
Value const* Value::find(const String& key) const {
return find(key.data(), key.data() + key.length());
}
Value* Value::demand(char const* begin, char const* end) {
JSON_ASSERT_MESSAGE(type() == nullValue || type() == objectValue,
"in Json::Value::demand(begin, end): requires "
Expand All @@ -1105,7 +1108,7 @@ const Value& Value::operator[](const char* key) const {
return *found;
}
Value const& Value::operator[](const String& key) const {
Value const* found = find(key.data(), key.data() + key.length());
Value const* found = find(key);
if (!found)
return nullSingleton();
return *found;
Expand Down
9 changes: 9 additions & 0 deletions src/test_lib_json/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,20 @@ JSONTEST_FIXTURE_LOCAL(ValueTest, objects) {
JSONTEST_ASSERT(foundId != nullptr);
JSONTEST_ASSERT_EQUAL(Json::Value(1234), *foundId);

const std::string stringIdKey = "id";
const Json::Value* stringFoundId = object1_.find(stringIdKey);
JSONTEST_ASSERT(stringFoundId != nullptr);
JSONTEST_ASSERT_EQUAL(Json::Value(1234), *stringFoundId);

const char unknownIdKey[] = "unknown id";
const Json::Value* foundUnknownId =
object1_.find(unknownIdKey, unknownIdKey + strlen(unknownIdKey));
JSONTEST_ASSERT_EQUAL(nullptr, foundUnknownId);

const std::string stringUnknownIdKey = "unknown id";
const Json::Value* stringFoundUnknownId = object1_.find(stringUnknownIdKey);
JSONTEST_ASSERT_EQUAL(nullptr, stringFoundUnknownId);

// Access through demand()
const char yetAnotherIdKey[] = "yet another id";
const Json::Value* foundYetAnotherId =
Expand Down

0 comments on commit 7f36cdb

Please sign in to comment.