Skip to content

Commit 4268ae5

Browse files
committed
add a new method to insert a new value in an array at specific index.
1 parent 5b91551 commit 4268ae5

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

include/json/value.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,8 @@ Json::Value obj_value(Json::objectValue); // {}
464464
/// Equivalent to jsonvalue[jsonvalue.size()] = value;
465465
Value& append(const Value& value);
466466
Value& append(Value&& value);
467-
467+
/// \brief Insert value in array at specific index
468+
bool insert(ArrayIndex index, const Value& newValue);
468469
/// Access an object value by name, create a null member if it does not exist.
469470
/// \note Because of our implementation, keys are limited to 2^30 -1 chars.
470471
/// Exceeding that will cause an exception.

src/lib_json/json_value.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,26 @@ Value& Value::append(const Value& value) { return (*this)[size()] = value; }
11741174
Value& Value::append(Value&& value) {
11751175
return (*this)[size()] = std::move(value);
11761176
}
1177+
/// \brief Insert value in array at specific index
1178+
bool Value::insert(ArrayIndex index, const Value& newValue){
1179+
if(type() != arrayValue){
1180+
return false;
1181+
}
1182+
if(!isValidIndex()){
1183+
return false;
1184+
}
1185+
ArrayIndex oldsize = size();
1186+
resize(oldsize+1);
1187+
ArrayIndex length = size();
1188+
if(length != oldsize +1){
1189+
return false;
1190+
}
1191+
for(ArrayIndex i = length ; i> index ; i--){
1192+
(*this)[i] = (*this)[i-1];
1193+
}
1194+
(*this)[index]= newValue;
1195+
return true;
1196+
}
11771197

11781198
Value Value::get(char const* begin,
11791199
char const* end,

src/test_lib_json/main.cpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,18 @@ JSONTEST_FIXTURE(ValueTest, arrayIssue252) {
307307
}
308308
// JSONTEST_ASSERT_EQUAL(5, root["array"].size());
309309
}
310-
310+
JSONTEST_FIXTURE(ValueTest, arrayIssue691) {
311+
Json::Value array2_;
312+
array2_.append(1);
313+
array2_.append(2);
314+
array2_.append(3);
315+
array2_.append(5);
316+
////use insert method
317+
array2_.insert(3,4);
318+
JSONTEST_ASSERT_EQUAL(Json::Value(3),array2_[2]);
319+
JSONTEST_ASSERT_EQUAL(Json::Value(4),array2_[3]);
320+
JSONTEST_ASSERT_EQUAL(Json::Value(5),array2_[4]);
321+
}
311322
JSONTEST_FIXTURE(ValueTest, null) {
312323
JSONTEST_ASSERT_EQUAL(Json::nullValue, null_.type());
313324

@@ -2535,6 +2546,7 @@ int main(int argc, const char* argv[]) {
25352546
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, objects);
25362547
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, arrays);
25372548
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, arrayIssue252);
2549+
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, arrayIssue691);
25382550
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, null);
25392551
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, strings);
25402552
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, bools);

0 commit comments

Comments
 (0)