Skip to content

Commit 9b0ba50

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

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

include/json/value.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,9 @@ Json::Value obj_value(Json::objectValue); // {}
463463
///
464464
/// Equivalent to jsonvalue[jsonvalue.size()] = value;
465465
Value& append(const Value& value);
466+
/// \brief Insert value in array at specific index
467+
bool insert(ArrayIndex index, const Value& newValue);
466468
Value& append(Value&& value);
467-
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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,26 @@ Value const& Value::operator[](CppTL::ConstString const& key) const {
11701170
#endif
11711171

11721172
Value& Value::append(const Value& value) { return (*this)[size()] = value; }
1173-
1173+
/// \brief Insert value in array at specific index
1174+
bool Value::insert(ArrayIndex index, const Value& newValue){
1175+
if(type() != arrayValue){
1176+
return false;
1177+
}
1178+
if(!isValidIndex()){
1179+
return false;
1180+
}
1181+
ArrayIndex oldsize = size();
1182+
resize(oldsize+1);
1183+
ArrayIndex length = size();
1184+
if(length != oldsize +1){
1185+
return false;
1186+
}
1187+
for(ArrayIndex i = length ; i> index ; i--){
1188+
(*this)[i] = (*this)[i-1];
1189+
}
1190+
(*this)[index]= newValue;
1191+
return true;
1192+
}
11741193
Value& Value::append(Value&& value) {
11751194
return (*this)[size()] = std::move(value);
11761195
}

src/test_lib_json/main.cpp

Lines changed: 13 additions & 1 deletion
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)