Skip to content

Commit 4a9f5c8

Browse files
committed
update.
1 parent 9b0ba50 commit 4a9f5c8

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

include/json/value.h

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

src/lib_json/json_value.cpp

+19-1
Original file line numberDiff line numberDiff line change
@@ -1193,7 +1193,25 @@ bool Value::insert(ArrayIndex index, const Value& newValue){
11931193
Value& Value::append(Value&& value) {
11941194
return (*this)[size()] = std::move(value);
11951195
}
1196-
1196+
bool Value::insert(ArrayIndex index, const Value&& newValue){
1197+
if(type() != arrayValue){
1198+
return false;
1199+
}
1200+
if(!isValidIndex()){
1201+
return false;
1202+
}
1203+
ArrayIndex oldsize = size();
1204+
resize(oldsize+1);
1205+
ArrayIndex length = size();
1206+
if(length != oldsize +1){
1207+
return false;
1208+
}
1209+
for(ArrayIndex i = length ; i> index ; i--){
1210+
(*this)[i] = (*this)[i-1];
1211+
}
1212+
(*this)[index]= std::move(newValue);
1213+
return true;
1214+
}
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)