Skip to content

add a new method to insert a new value in an array at specific index. #948

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/json/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,9 @@ Json::Value obj_value(Json::objectValue); // {}
Value& append(const Value& value);
Value& append(Value&& value);

/// \brief Insert value in array at specific index
bool Value::insert(ArrayIndex index, const Value& newValue);

/// Access an object value by name, create a null member if it does not exist.
/// \note Because of our implementation, keys are limited to 2^30 -1 chars.
/// Exceeding that will cause an exception.
Expand Down
21 changes: 20 additions & 1 deletion src/lib_json/json_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,26 @@ Value Value::get(char const* key, Value const& defaultValue) const {
Value Value::get(String const& key, Value const& defaultValue) const {
return get(key.data(), key.data() + key.length(), defaultValue);
}

/// \brief Insert value in array at specific index
bool Value::insert(ArrayIndex index, const Value& newValue){
if(type() != arrayValue){
return false;
}
if(!isValidIndex()){
return false;
}
ArrayIndex oldsize = size();
resize(oldsize+1);
ArrayIndex length = size();
if(length != oldsize +1){
return false;
}
for(ArrayIndex i = length ; i> index ; i--){
(*this)[i] = (*this)[i-1];
}
(*this)[index]= newValue;
return true;
}
bool Value::removeMember(const char* begin, const char* end, Value* removed) {
if (type() != objectValue) {
return false;
Expand Down
14 changes: 13 additions & 1 deletion src/test_lib_json/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,18 @@ JSONTEST_FIXTURE(ValueTest, arrayIssue252) {
}
// JSONTEST_ASSERT_EQUAL(5, root["array"].size());
}

JSONTEST_FIXTURE(ValueTest, arrayIssue691) {
Json::Value array2_;
array2_.append(1);
array2_.append(2);
array2_.append(3);
array2_.append(5);
////use insert method
array2_.insert(3,4);
JSONTEST_ASSERT_EQUAL(Json::Value(3),array2_[2]);
JSONTEST_ASSERT_EQUAL(Json::Value(4),array2_[3]);
JSONTEST_ASSERT_EQUAL(Json::Value(5),array2_[4]);
}
JSONTEST_FIXTURE(ValueTest, null) {
JSONTEST_ASSERT_EQUAL(Json::nullValue, null_.type());

Expand Down Expand Up @@ -2535,6 +2546,7 @@ int main(int argc, const char* argv[]) {
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, objects);
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, arrays);
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, arrayIssue252);
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, arrayIssue691);
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, null);
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, strings);
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, bools);
Expand Down