Skip to content

Commit 589b601

Browse files
committed
update
1 parent 7095282 commit 589b601

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

include/json/value.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ Json::Value obj_value(Json::objectValue); // {}
467467
bool insert(ArrayIndex index, const Value& newValue);
468468
#if JSON_HAS_RVALUE_REFERENCES
469469
Value& append(Value&& value);
470-
bool insert(ArrayIndex index, const Value&& newValue);
470+
bool insert(ArrayIndex index, Value&& newValue);
471471
#endif
472472

473473
/// Access an object value by name, create a null member if it does not exist.

src/lib_json/json_value.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,8 +1182,9 @@ bool Value::insert(ArrayIndex index, const Value& newValue) {
11821182
resize(oldsize + 1);
11831183
ArrayIndex length = size();
11841184

1185-
for(ArrayIndex i = length ; i> index ; i--) {
1186-
(*this)[i] = (*this)[i-1];
1185+
for (ArrayIndex i = length; i > index; i--) {
1186+
CZString key(i);
1187+
(*value_.map_)[key] = (*this)[i - 1];
11871188
}
11881189
(*this)[index] = newValue;
11891190
return true;
@@ -1192,7 +1193,7 @@ bool Value::insert(ArrayIndex index, const Value& newValue) {
11921193
Value& Value::append(Value&& value) {
11931194
return (*this)[size()] = std::move(value);
11941195
}
1195-
bool Value::insert(ArrayIndex index, const Value&& newValue) {
1196+
bool Value::insert(ArrayIndex index, Value&& newValue) {
11961197
if (type() != arrayValue) {
11971198
return false;
11981199
}
@@ -1203,8 +1204,9 @@ bool Value::insert(ArrayIndex index, const Value&& newValue) {
12031204
resize(oldsize + 1);
12041205
ArrayIndex length = size();
12051206

1206-
for(ArrayIndex i = length ; i> index ; i--) {
1207-
(*this)[i] = (*this)[i-1];
1207+
for (ArrayIndex i = length; i > index; i--) {
1208+
CZString key(i);
1209+
(*value_.map_)[key] = std::move((*this)[i - 1]);
12081210
}
12091211
(*this)[index] = std::move(newValue);
12101212
return true;

src/test_lib_json/main.cpp

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -313,30 +313,30 @@ JSONTEST_FIXTURE(ValueTest, arrayIssue252) {
313313
JSONTEST_FIXTURE(ValueTest, arrayInsertAtRandomIndex) {
314314
Json::Value array;
315315
JSONCPP_STRING str = "index5";
316-
array.append("index0");
317-
array.append("index1");
318-
array.append("index2");
319-
array.append("index3");
320-
321-
JSONTEST_ASSERT_EQUAL(Json::Value("index0"),array[0]);
322-
JSONTEST_ASSERT_EQUAL(Json::Value("index1"),array[1]);
323-
JSONTEST_ASSERT_EQUAL(Json::Value("index2"),array[2]);
324-
JSONTEST_ASSERT_EQUAL(Json::Value("index3"),array[3]);
325-
326-
array.insert(3,"index4"); //rvalue
327-
JSONTEST_ASSERT_EQUAL(Json::Value("index0"),array[0]);
328-
JSONTEST_ASSERT_EQUAL(Json::Value("index1"),array[1]);
329-
JSONTEST_ASSERT_EQUAL(Json::Value("index2"),array[2]);
330-
JSONTEST_ASSERT_EQUAL(Json::Value("index4"),array[3]);
331-
JSONTEST_ASSERT_EQUAL(Json::Value("index3"),array[4]);
332-
333-
array.insert(4,str); //lvalue
334-
JSONTEST_ASSERT_EQUAL(Json::Value("index0"),array[0]);
335-
JSONTEST_ASSERT_EQUAL(Json::Value("index1"),array[1]);
336-
JSONTEST_ASSERT_EQUAL(Json::Value("index2"),array[2]);
337-
JSONTEST_ASSERT_EQUAL(Json::Value("index4"),array[3]);
338-
JSONTEST_ASSERT_EQUAL(Json::Value("index5"),array[4]);
339-
JSONTEST_ASSERT_EQUAL(Json::Value("index3"),array[5]);
316+
array.append("index0");
317+
array.append("index1");
318+
array.append("index2");
319+
array.append("index3");
320+
321+
JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]);
322+
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]);
323+
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]);
324+
JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[3]);
325+
326+
array.insert(3, "index4"); // rvalue
327+
JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]);
328+
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]);
329+
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]);
330+
JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[3]);
331+
JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[4]);
332+
333+
array.insert(4, str); // lvalue
334+
JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]);
335+
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]);
336+
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]);
337+
JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[3]);
338+
JSONTEST_ASSERT_EQUAL(Json::Value("index5"), array[4]);
339+
JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[5]);
340340
}
341341
JSONTEST_FIXTURE(ValueTest, null) {
342342
JSONTEST_ASSERT_EQUAL(Json::nullValue, null_.type());

0 commit comments

Comments
 (0)