Skip to content

Commit

Permalink
Merge pull request #135 from cdunn2001/removeMember
Browse files Browse the repository at this point in the history
Deprecate old `removeMember()`. Add new.

[Deprecated methods will be removed at the next major version bump](http://apr.apache.org/versioning.html#binary).
  • Loading branch information
cdunn2001 committed Jan 23, 2015
2 parents 70b795b + 9132aa9 commit ee8b58f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 19 deletions.
9 changes: 8 additions & 1 deletion include/json/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -392,14 +392,21 @@ Json::Value obj_value(Json::objectValue); // {}
/// \return the removed Value, or null.
/// \pre type() is objectValue or nullValue
/// \post type() is unchanged
/// \deprecated
Value removeMember(const char* key);
/// Same as removeMember(const char*)
/// \deprecated
Value removeMember(const std::string& key);
/** \brief Remove the named map member.
Update 'removed' iff removed.
\return true iff removed (no exceptions)
*/
bool removeMember(const char* key, Value* removed);
/** \brief Remove the indexed array element.
O(n) expensive operations.
Update 'removed' iff removed.
(This is a better pattern than removeMember().)
\return true iff removed (no exceptions)
*/
bool removeIndex(ArrayIndex i, Value* removed);
Expand Down
6 changes: 3 additions & 3 deletions include/json/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
#ifndef JSON_VERSION_H_INCLUDED
# define JSON_VERSION_H_INCLUDED

# define JSONCPP_VERSION_STRING "1.2.1"
# define JSONCPP_VERSION_STRING "1.3.0"
# define JSONCPP_VERSION_MAJOR 1
# define JSONCPP_VERSION_MINOR 2
# define JSONCPP_VERSION_PATCH 1
# define JSONCPP_VERSION_MINOR 3
# define JSONCPP_VERSION_PATCH 0
# define JSONCPP_VERSION_QUALIFIER
# define JSONCPP_VERSION_HEXA ((JSONCPP_VERSION_MAJOR << 24) | (JSONCPP_VERSION_MINOR << 16) | (JSONCPP_VERSION_PATCH << 8))

Expand Down
33 changes: 22 additions & 11 deletions src/lib_json/json_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -989,31 +989,42 @@ Value Value::get(const std::string& key, const Value& defaultValue) const {
return get(key.c_str(), defaultValue);
}

Value Value::removeMember(const char* key) {
JSON_ASSERT_MESSAGE(type_ == nullValue || type_ == objectValue,
"in Json::Value::removeMember(): requires objectValue");
if (type_ == nullValue)
return null;

bool Value::removeMember(const char* key, Value* removed) {
if (type_ != objectValue) {
return false;
}
#ifndef JSON_VALUE_USE_INTERNAL_MAP
CZString actualKey(key, CZString::noDuplication);
ObjectValues::iterator it = value_.map_->find(actualKey);
if (it == value_.map_->end())
return null;
Value old(it->second);
return false;
*removed = it->second;
value_.map_->erase(it);
return old;
return true;
#else
Value* value = value_.map_->find(key);
if (value) {
Value old(*value);
*removed = *value;
value_.map_.remove(key);
return old;
return true;
} else {
return null;
return false;
}
#endif
}

Value Value::removeMember(const char* key) {
JSON_ASSERT_MESSAGE(type_ == nullValue || type_ == objectValue,
"in Json::Value::removeMember(): requires objectValue");
if (type_ == nullValue)
return null;

Value removed; // null
removeMember(key, &removed);
return removed; // still null if removeMember() did nothing
}

Value Value::removeMember(const std::string& key) {
return removeMember(key.c_str());
}
Expand Down
10 changes: 7 additions & 3 deletions src/test_lib_json/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,14 @@ JSONTEST_FIXTURE(ValueTest, objects) {

// Remove.
Json::Value got;
got = object1_.removeMember("some other id");
bool did;
did = object1_.removeMember("some other id", &got);
JSONTEST_ASSERT_EQUAL(Json::Value("foo"), got);
got = object1_.removeMember("some other id");
JSONTEST_ASSERT_EQUAL(Json::nullValue, got);
JSONTEST_ASSERT_EQUAL(true, did);
got = Json::Value("bar");
did = object1_.removeMember("some other id", &got);
JSONTEST_ASSERT_EQUAL(Json::Value("bar"), got);
JSONTEST_ASSERT_EQUAL(false, did);
}

JSONTEST_FIXTURE(ValueTest, arrays) {
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.1
1.3.0

0 comments on commit ee8b58f

Please sign in to comment.