Skip to content

Commit 7f5b023

Browse files
committed
add movable insert method.
1 parent 9b0ba50 commit 7f5b023

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

include/json/value.h

+4
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,11 @@ Json::Value obj_value(Json::objectValue); // {}
465465
Value& append(const Value& value);
466466
/// \brief Insert value in array at specific index
467467
bool insert(ArrayIndex index, const Value& newValue);
468+
#if JSON_HAS_RVALUE_REFERENCES
468469
Value& append(Value&& value);
470+
bool insert(ArrayIndex index, const Value&& newValue);
471+
#endif
472+
469473
/// Access an object value by name, create a null member if it does not exist.
470474
/// \note Because of our implementation, keys are limited to 2^30 -1 chars.
471475
/// Exceeding that will cause an exception.

src/lib_json/json_value.cpp

+22-4
Original file line numberDiff line numberDiff line change
@@ -1175,7 +1175,7 @@ bool Value::insert(ArrayIndex index, const Value& newValue){
11751175
if(type() != arrayValue){
11761176
return false;
11771177
}
1178-
if(!isValidIndex()){
1178+
if(!isValidIndex(index)){
11791179
return false;
11801180
}
11811181
ArrayIndex oldsize = size();
@@ -1190,10 +1190,28 @@ bool Value::insert(ArrayIndex index, const Value& newValue){
11901190
(*this)[index]= newValue;
11911191
return true;
11921192
}
1193-
Value& Value::append(Value&& value) {
1194-
return (*this)[size()] = std::move(value);
1193+
#if JSON_HAS_RVALUE_REFERENCES
1194+
Value& Value::append(Value&& value) { return (*this)[size()] = std::move(value); }
1195+
bool Value::insert(ArrayIndex index, const Value&& newValue){
1196+
if(type() != arrayValue){
1197+
return false;
1198+
}
1199+
if(!isValidIndex(index)){
1200+
return false;
1201+
}
1202+
ArrayIndex oldsize = size();
1203+
resize(oldsize+1);
1204+
ArrayIndex length = size();
1205+
if(length != oldsize +1){
1206+
return false;
1207+
}
1208+
for(ArrayIndex i = length ; i> index ; i--){
1209+
(*this)[i] = (*this)[i-1];
1210+
}
1211+
(*this)[index]= std::move(newValue);
1212+
return true;
11951213
}
1196-
1214+
#endif
11971215
Value Value::get(char const* begin,
11981216
char const* end,
11991217
Value const& defaultValue) const {

src/test_lib_json/main.cpp

+12-10
Original file line numberDiff line numberDiff line change
@@ -308,16 +308,18 @@ JSONTEST_FIXTURE(ValueTest, arrayIssue252) {
308308
// JSONTEST_ASSERT_EQUAL(5, root["array"].size());
309309
}
310310
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]);
311+
Json::Value array2;
312+
array2.append(10); // index 0
313+
array2.append(20); // index 1
314+
array2.append(30); // index 2
315+
array2.append(50); // index 3
316+
317+
JSONTEST_ASSERT_EQUAL(Json::Value(50),array2[3]);
318+
319+
array2.insert(3,40); // index 3
320+
// After updating, index 3 should be changed from 50 to 40, and 50 moved to index 4.
321+
JSONTEST_ASSERT_EQUAL(Json::Value(40),array2[3]);
322+
JSONTEST_ASSERT_EQUAL(Json::Value(50),array2[4]);
321323
}
322324
JSONTEST_FIXTURE(ValueTest, null) {
323325
JSONTEST_ASSERT_EQUAL(Json::nullValue, null_.type());

0 commit comments

Comments
 (0)