File tree 3 files changed +38
-14
lines changed
3 files changed +38
-14
lines changed Original file line number Diff line number Diff line change @@ -465,7 +465,11 @@ Json::Value obj_value(Json::objectValue); // {}
465
465
Value& append (const Value& value);
466
466
// / \brief Insert value in array at specific index
467
467
bool insert (ArrayIndex index, const Value& newValue);
468
+ #if JSON_HAS_RVALUE_REFERENCES
468
469
Value& append (Value&& value);
470
+ bool insert (ArrayIndex index, const Value&& newValue);
471
+ #endif
472
+
469
473
// / Access an object value by name, create a null member if it does not exist.
470
474
// / \note Because of our implementation, keys are limited to 2^30 -1 chars.
471
475
// / Exceeding that will cause an exception.
Original file line number Diff line number Diff line change @@ -1175,7 +1175,7 @@ bool Value::insert(ArrayIndex index, const Value& newValue){
1175
1175
if (type () != arrayValue){
1176
1176
return false ;
1177
1177
}
1178
- if (!isValidIndex ()){
1178
+ if (!isValidIndex (index )){
1179
1179
return false ;
1180
1180
}
1181
1181
ArrayIndex oldsize = size ();
@@ -1190,10 +1190,28 @@ bool Value::insert(ArrayIndex index, const Value& newValue){
1190
1190
(*this )[index ]= newValue;
1191
1191
return true ;
1192
1192
}
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 ;
1195
1213
}
1196
-
1214
+ # endif
1197
1215
Value Value::get (char const * begin,
1198
1216
char const * end,
1199
1217
Value const & defaultValue) const {
Original file line number Diff line number Diff line change @@ -308,16 +308,18 @@ JSONTEST_FIXTURE(ValueTest, arrayIssue252) {
308
308
// JSONTEST_ASSERT_EQUAL(5, root["array"].size());
309
309
}
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 ]);
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 ]);
321
323
}
322
324
JSONTEST_FIXTURE (ValueTest, null) {
323
325
JSONTEST_ASSERT_EQUAL (Json::nullValue, null_.type ());
You can’t perform that action at this time.
0 commit comments