Skip to content

Commit 7f36cdb

Browse files
petukhovtdbaylesjPetukhov Timofey
authored
Added Value::find with String key (#1467)
* 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>
1 parent 2067f66 commit 7f36cdb

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

include/json/value.h

+3
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,9 @@ class JSON_API Value {
513513
/// and operator[]const
514514
/// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
515515
Value const* find(char const* begin, char const* end) const;
516+
/// Most general and efficient version of isMember()const, get()const,
517+
/// and operator[]const
518+
Value const* find(const String& key) const;
516519
/// Most general and efficient version of object-mutators.
517520
/// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
518521
/// \return non-zero, but JSON_ASSERT if this is neither object nor nullValue.

src/lib_json/json_value.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,9 @@ Value const* Value::find(char const* begin, char const* end) const {
10921092
return nullptr;
10931093
return &(*it).second;
10941094
}
1095+
Value const* Value::find(const String& key) const {
1096+
return find(key.data(), key.data() + key.length());
1097+
}
10951098
Value* Value::demand(char const* begin, char const* end) {
10961099
JSON_ASSERT_MESSAGE(type() == nullValue || type() == objectValue,
10971100
"in Json::Value::demand(begin, end): requires "
@@ -1105,7 +1108,7 @@ const Value& Value::operator[](const char* key) const {
11051108
return *found;
11061109
}
11071110
Value const& Value::operator[](const String& key) const {
1108-
Value const* found = find(key.data(), key.data() + key.length());
1111+
Value const* found = find(key);
11091112
if (!found)
11101113
return nullSingleton();
11111114
return *found;

src/test_lib_json/main.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,20 @@ JSONTEST_FIXTURE_LOCAL(ValueTest, objects) {
220220
JSONTEST_ASSERT(foundId != nullptr);
221221
JSONTEST_ASSERT_EQUAL(Json::Value(1234), *foundId);
222222

223+
const std::string stringIdKey = "id";
224+
const Json::Value* stringFoundId = object1_.find(stringIdKey);
225+
JSONTEST_ASSERT(stringFoundId != nullptr);
226+
JSONTEST_ASSERT_EQUAL(Json::Value(1234), *stringFoundId);
227+
223228
const char unknownIdKey[] = "unknown id";
224229
const Json::Value* foundUnknownId =
225230
object1_.find(unknownIdKey, unknownIdKey + strlen(unknownIdKey));
226231
JSONTEST_ASSERT_EQUAL(nullptr, foundUnknownId);
227232

233+
const std::string stringUnknownIdKey = "unknown id";
234+
const Json::Value* stringFoundUnknownId = object1_.find(stringUnknownIdKey);
235+
JSONTEST_ASSERT_EQUAL(nullptr, stringFoundUnknownId);
236+
228237
// Access through demand()
229238
const char yetAnotherIdKey[] = "yet another id";
230239
const Json::Value* foundYetAnotherId =

0 commit comments

Comments
 (0)