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