mirror of
https://github.com/open-source-parsers/jsoncpp.git
synced 2025-04-27 18:30:50 +02:00
Merge pull request #202 from open-source-parsers/get-with-zero
`Value::get(key, default)` with zero
This commit is contained in:
commit
7ec98dc9fe
@ -404,15 +404,19 @@ Json::Value obj_value(Json::objectValue); // {}
|
||||
const Value& operator[](const CppTL::ConstString& key) const;
|
||||
#endif
|
||||
/// Return the member named key if it exist, defaultValue otherwise.
|
||||
/// \note deep copy
|
||||
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 char* key, const char* end, 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 std::string& key, const Value& defaultValue) const;
|
||||
#ifdef JSON_USE_CPPTL
|
||||
/// Return the member named key if it exist, defaultValue otherwise.
|
||||
/// \note deep copy
|
||||
Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
|
||||
#endif
|
||||
/// Most general and efficient version of isMember()const, get()const,
|
||||
|
@ -1028,8 +1028,8 @@ Value& Value::append(const Value& value) { return (*this)[size()] = value; }
|
||||
|
||||
Value Value::get(char const* key, char const* end, Value const& defaultValue) const
|
||||
{
|
||||
const Value* value = &((*this)[key]);
|
||||
return value == &nullRef ? defaultValue : *value;
|
||||
Value const* found = find(key, end);
|
||||
return !found ? defaultValue : *found;
|
||||
}
|
||||
Value Value::get(char const* key, Value const& defaultValue) const
|
||||
{
|
||||
@ -1037,7 +1037,7 @@ Value Value::get(char const* key, Value const& defaultValue) const
|
||||
}
|
||||
Value Value::get(std::string const& key, Value const& defaultValue) const
|
||||
{
|
||||
return get(key.c_str(), defaultValue);
|
||||
return get(key.data(), key.data() + key.length(), defaultValue);
|
||||
}
|
||||
|
||||
|
||||
@ -1104,7 +1104,7 @@ bool Value::removeIndex(ArrayIndex index, Value* removed) {
|
||||
#ifdef JSON_USE_CPPTL
|
||||
Value Value::get(const CppTL::ConstString& key,
|
||||
const Value& defaultValue) const {
|
||||
return get(key.c_str(), defaultValue);
|
||||
return get(key.c_str(), key.end_c_str(), defaultValue);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -1578,6 +1578,7 @@ JSONTEST_FIXTURE(ValueTest, zeroesInKeys) {
|
||||
JSONTEST_ASSERT_STRING_EQUAL("there", root[binary].asString());
|
||||
JSONTEST_ASSERT(!root.isMember("hi"));
|
||||
JSONTEST_ASSERT(root.isMember(binary));
|
||||
JSONTEST_ASSERT_STRING_EQUAL("there", root.get(binary, Json::Value::nullRef).asString());
|
||||
Json::Value removed;
|
||||
bool did;
|
||||
did = root.removeMember(binary.data(), binary.data() + binary.length(),
|
||||
@ -1588,6 +1589,8 @@ JSONTEST_FIXTURE(ValueTest, zeroesInKeys) {
|
||||
&removed);
|
||||
JSONTEST_ASSERT(!did);
|
||||
JSONTEST_ASSERT_STRING_EQUAL("there", removed.asString()); // still
|
||||
JSONTEST_ASSERT(!root.isMember(binary));
|
||||
JSONTEST_ASSERT_STRING_EQUAL("", root.get(binary, Json::Value::nullRef).asString());
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user