From 9de2c2d84d3655c76db876cd38c1faf23126422d Mon Sep 17 00:00:00 2001 From: Christopher Dunn Date: Tue, 20 Jan 2015 16:15:40 -0600 Subject: [PATCH 1/5] partial --- include/json/value.h | 9 +++++++++ src/lib_json/json_value.cpp | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/include/json/value.h b/include/json/value.h index 78e7546..18355d0 100644 --- a/include/json/value.h +++ b/include/json/value.h @@ -395,6 +395,15 @@ Json::Value obj_value(Json::objectValue); // {} Value removeMember(const char* key); /// Same as removeMember(const char*) Value removeMember(const std::string& key); + /** \brief Remove the indexed array element. + + O(n) expensive operations. + Update 'removed' iff removed. + (This is a better pattern than removeMember().) + JSON_FAIL if !isValidIndex(i) or if not arrayObject + \return true iff removed + */ + bool removeIndex(ArrayIndex i, Value* removed); /// Return true if the object has a member named key. bool isMember(const char* key) const; diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index 0a7fb85..aa9bfb4 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -1018,6 +1018,14 @@ Value Value::removeMember(const std::string& key) { return removeMember(key.c_str()); } +bool Value::removeIndex(ArrayIndex i, Value* removed) { + JSON_ASSERT_MESSAGE(this->type_ == arrayValue, + "in Json::Value::removeIndex(): requires arrayValue"); + JSON_ASSERT_MESSAGE(this->isValidIndex(i), + "invalid index i=" << i << " for array of size " << this->size()); + return true; +} + #ifdef JSON_USE_CPPTL Value Value::get(const CppTL::ConstString& key, const Value& defaultValue) const { From e87e41cdb0b26aceecdda451755b1237a4307979 Mon Sep 17 00:00:00 2001 From: Christopher Dunn Date: Tue, 20 Jan 2015 16:24:11 -0600 Subject: [PATCH 2/5] from Itzik S; see issue #28 with minor corrections --- include/json/value.h | 3 +-- src/lib_json/json_value.cpp | 30 +++++++++++++++++++++++++----- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/include/json/value.h b/include/json/value.h index 18355d0..c4169b4 100644 --- a/include/json/value.h +++ b/include/json/value.h @@ -400,8 +400,7 @@ Json::Value obj_value(Json::objectValue); // {} O(n) expensive operations. Update 'removed' iff removed. (This is a better pattern than removeMember().) - JSON_FAIL if !isValidIndex(i) or if not arrayObject - \return true iff removed + \return true iff removed (no exceptions) */ bool removeIndex(ArrayIndex i, Value* removed); diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index aa9bfb4..f9e733f 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -1018,12 +1018,32 @@ Value Value::removeMember(const std::string& key) { return removeMember(key.c_str()); } -bool Value::removeIndex(ArrayIndex i, Value* removed) { - JSON_ASSERT_MESSAGE(this->type_ == arrayValue, - "in Json::Value::removeIndex(): requires arrayValue"); - JSON_ASSERT_MESSAGE(this->isValidIndex(i), - "invalid index i=" << i << " for array of size " << this->size()); +bool Value::removeIndex(ArrayIndex index, Value* removed) { + if (this->type_ != arrayValue) { + return false; + } +#ifdef JSON_VALUE_USE_INTERNAL_MAP + JSON_FAIL_MESSAGE("removeIndex is not implemented for ValueInternalArray."); + return false; +#else + CZString key(index); + ObjectValues::iterator it = this->value_.map_->find(key); + if (it == this->value_.map_->end()) { + return false; + } + *removed = it->second; + ArrayIndex oldSize = this->size(); + // shift left all items left, into the place of the "removed" + for (ArrayIndex i=index; ivalue_.map_)[key] = (*this)[i+1]; + } + // erase the last one ("leftover") + CZString keyLast(oldSize-1); + ObjectValues::iterator itLast = this->value_.map_->find(keyLast); + this->value_.map_->erase(itLast); return true; +#endif } #ifdef JSON_USE_CPPTL From e893625e88f02539b9cd03e591df3ee8970552c3 Mon Sep 17 00:00:00 2001 From: Christopher Dunn Date: Tue, 20 Jan 2015 16:54:27 -0600 Subject: [PATCH 3/5] test removeIndex/Member() --- src/test_lib_json/main.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index 08ef66a..98fb8d0 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -198,6 +198,14 @@ JSONTEST_FIXTURE(ValueTest, objects) { object1_["some other id"] = "foo"; JSONTEST_ASSERT_EQUAL(Json::Value("foo"), object1_["some other id"]); + JSONTEST_ASSERT_EQUAL(Json::Value("foo"), object1_["some other id"]); + + // Remove. + Json::Value got; + got = object1_.removeMember("some other id"); + JSONTEST_ASSERT_EQUAL(Json::Value("foo"), got); + got = object1_.removeMember("some other id"); + JSONTEST_ASSERT_EQUAL(Json::nullValue, got); } JSONTEST_FIXTURE(ValueTest, arrays) { @@ -240,6 +248,10 @@ JSONTEST_FIXTURE(ValueTest, arrays) { array1_[2] = Json::Value(17); JSONTEST_ASSERT_EQUAL(Json::Value(), array1_[1]); JSONTEST_ASSERT_EQUAL(Json::Value(17), array1_[2]); + Json::Value got; + JSONTEST_ASSERT_EQUAL(true, array1_.removeIndex(2, &got)); + JSONTEST_ASSERT_EQUAL(Json::Value(17), got); + JSONTEST_ASSERT_EQUAL(false, array1_.removeIndex(2, &got)); // gone now } JSONTEST_FIXTURE(ValueTest, null) { From 05c1b8344d8149f1b9f5b6ec7ca59af952c18da2 Mon Sep 17 00:00:00 2001 From: Christopher Dunn Date: Wed, 21 Jan 2015 15:43:48 -0600 Subject: [PATCH 4/5] drop this-> (team preference) --- src/lib_json/json_value.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index f9e733f..da5e35a 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -1019,7 +1019,7 @@ Value Value::removeMember(const std::string& key) { } bool Value::removeIndex(ArrayIndex index, Value* removed) { - if (this->type_ != arrayValue) { + if (type_ != arrayValue) { return false; } #ifdef JSON_VALUE_USE_INTERNAL_MAP @@ -1027,21 +1027,21 @@ bool Value::removeIndex(ArrayIndex index, Value* removed) { return false; #else CZString key(index); - ObjectValues::iterator it = this->value_.map_->find(key); - if (it == this->value_.map_->end()) { + ObjectValues::iterator it = value_.map_->find(key); + if (it == value_.map_->end()) { return false; } *removed = it->second; - ArrayIndex oldSize = this->size(); + ArrayIndex oldSize = size(); // shift left all items left, into the place of the "removed" for (ArrayIndex i=index; ivalue_.map_)[key] = (*this)[i+1]; + (*value_.map_)[key] = (*this)[i+1]; } // erase the last one ("leftover") CZString keyLast(oldSize-1); - ObjectValues::iterator itLast = this->value_.map_->find(keyLast); - this->value_.map_->erase(itLast); + ObjectValues::iterator itLast = value_.map_->find(keyLast); + value_.map_->erase(itLast); return true; #endif } From 59167d86271437ddd24e017c2d2d66893568a057 Mon Sep 17 00:00:00 2001 From: Christopher Dunn Date: Wed, 21 Jan 2015 16:04:46 -0600 Subject: [PATCH 5/5] more changes per cr --- src/lib_json/json_value.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index da5e35a..7cc65bd 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -1034,12 +1034,12 @@ bool Value::removeIndex(ArrayIndex index, Value* removed) { *removed = it->second; ArrayIndex oldSize = size(); // shift left all items left, into the place of the "removed" - for (ArrayIndex i=index; ifind(keyLast); value_.map_->erase(itLast); return true;