mirror of
https://github.com/open-source-parsers/jsoncpp.git
synced 2024-12-26 02:00:50 +08:00
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>
This commit is contained in:
parent
2067f66d66
commit
7f36cdb3ea
@ -513,6 +513,9 @@ public:
|
||||
/// 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.
|
||||
|
@ -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 "
|
||||
@ -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;
|
||||
|
@ -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 =
|
||||
|
Loading…
x
Reference in New Issue
Block a user