diff --git a/include/json/value.h b/include/json/value.h index c4169b4..b7ac2ad 100644 --- a/include/json/value.h +++ b/include/json/value.h @@ -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); diff --git a/include/json/version.h b/include/json/version.h index a46c704..37e082b 100644 --- a/include/json/version.h +++ b/include/json/version.h @@ -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)) diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index 9ca0920..d646055 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -989,29 +989,40 @@ Value Value::get(const std::string& key, const Value& defaultValue) const { return get(key.c_str(), defaultValue); } + +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 false; + *removed = it->second; + value_.map_->erase(it); + return true; +#else + Value* value = value_.map_->find(key); + if (value) { + *removed = *value; + value_.map_.remove(key); + return true; + } else { + 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; -#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); - value_.map_->erase(it); - return old; -#else - Value* value = value_.map_->find(key); - if (value) { - Value old(*value); - value_.map_.remove(key); - return old; - } else { - return null; - } -#endif + + Value removed; // null + removeMember(key, &removed); + return removed; // still null if removeMember() did nothing } Value Value::removeMember(const std::string& key) { diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index 98fb8d0..8af3e19 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -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) { diff --git a/version b/version index 998dccf..589268e 100644 --- a/version +++ b/version @@ -1 +1 @@ -1.2.1 \ No newline at end of file +1.3.0 \ No newline at end of file