Skip to content

Commit 7ec98dc

Browse files
committed
Merge pull request open-source-parsers#202 from open-source-parsers/get-with-zero
`Value::get(key, default)` with zero
2 parents f50145f + 0fd2875 commit 7ec98dc

3 files changed

Lines changed: 11 additions & 4 deletions

File tree

include/json/value.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,15 +404,19 @@ Json::Value obj_value(Json::objectValue); // {}
404404
const Value& operator[](const CppTL::ConstString& key) const;
405405
#endif
406406
/// Return the member named key if it exist, defaultValue otherwise.
407+
/// \note deep copy
407408
Value get(const char* key, const Value& defaultValue) const;
408409
/// Return the member named key if it exist, defaultValue otherwise.
410+
/// \note deep copy
409411
/// \param key may contain embedded nulls.
410412
Value get(const char* key, const char* end, const Value& defaultValue) const;
411413
/// Return the member named key if it exist, defaultValue otherwise.
414+
/// \note deep copy
412415
/// \param key may contain embedded nulls.
413416
Value get(const std::string& key, const Value& defaultValue) const;
414417
#ifdef JSON_USE_CPPTL
415418
/// Return the member named key if it exist, defaultValue otherwise.
419+
/// \note deep copy
416420
Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
417421
#endif
418422
/// Most general and efficient version of isMember()const, get()const,

src/lib_json/json_value.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,16 +1028,16 @@ Value& Value::append(const Value& value) { return (*this)[size()] = value; }
10281028

10291029
Value Value::get(char const* key, char const* end, Value const& defaultValue) const
10301030
{
1031-
const Value* value = &((*this)[key]);
1032-
return value == &nullRef ? defaultValue : *value;
1031+
Value const* found = find(key, end);
1032+
return !found ? defaultValue : *found;
10331033
}
10341034
Value Value::get(char const* key, Value const& defaultValue) const
10351035
{
10361036
return get(key, key + strlen(key), defaultValue);
10371037
}
10381038
Value Value::get(std::string const& key, Value const& defaultValue) const
10391039
{
1040-
return get(key.c_str(), defaultValue);
1040+
return get(key.data(), key.data() + key.length(), defaultValue);
10411041
}
10421042

10431043

@@ -1104,7 +1104,7 @@ bool Value::removeIndex(ArrayIndex index, Value* removed) {
11041104
#ifdef JSON_USE_CPPTL
11051105
Value Value::get(const CppTL::ConstString& key,
11061106
const Value& defaultValue) const {
1107-
return get(key.c_str(), defaultValue);
1107+
return get(key.c_str(), key.end_c_str(), defaultValue);
11081108
}
11091109
#endif
11101110

src/test_lib_json/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,6 +1578,7 @@ JSONTEST_FIXTURE(ValueTest, zeroesInKeys) {
15781578
JSONTEST_ASSERT_STRING_EQUAL("there", root[binary].asString());
15791579
JSONTEST_ASSERT(!root.isMember("hi"));
15801580
JSONTEST_ASSERT(root.isMember(binary));
1581+
JSONTEST_ASSERT_STRING_EQUAL("there", root.get(binary, Json::Value::nullRef).asString());
15811582
Json::Value removed;
15821583
bool did;
15831584
did = root.removeMember(binary.data(), binary.data() + binary.length(),
@@ -1588,6 +1589,8 @@ JSONTEST_FIXTURE(ValueTest, zeroesInKeys) {
15881589
&removed);
15891590
JSONTEST_ASSERT(!did);
15901591
JSONTEST_ASSERT_STRING_EQUAL("there", removed.asString()); // still
1592+
JSONTEST_ASSERT(!root.isMember(binary));
1593+
JSONTEST_ASSERT_STRING_EQUAL("", root.get(binary, Json::Value::nullRef).asString());
15911594
}
15921595
}
15931596

0 commit comments

Comments
 (0)