Skip to content

Commit a0bd9ad

Browse files
authored
Add an insert overload function (open-source-parsers#1110)
1 parent 9e0d70a commit a0bd9ad

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

include/json/value.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,10 @@ class JSON_API Value {
445445
/// Equivalent to jsonvalue[jsonvalue.size()] = value;
446446
Value& append(const Value& value);
447447
Value& append(Value&& value);
448+
448449
/// \brief Insert value in array at specific index
449-
bool insert(ArrayIndex index, Value newValue);
450+
bool insert(ArrayIndex index, const Value& newValue);
451+
bool insert(ArrayIndex index, Value&& newValue);
450452

451453
/// Access an object value by name, create a null member if it does not exist.
452454
/// \note Because of our implementation, keys are limited to 2^30 -1 chars.

src/lib_json/json_value.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,11 @@ Value& Value::append(Value&& value) {
11261126
return this->value_.map_->emplace(size(), std::move(value)).first->second;
11271127
}
11281128

1129-
bool Value::insert(ArrayIndex index, Value newValue) {
1129+
bool Value::insert(ArrayIndex index, const Value& newValue) {
1130+
return insert(index, Value(newValue));
1131+
}
1132+
1133+
bool Value::insert(ArrayIndex index, Value&& newValue) {
11301134
JSON_ASSERT_MESSAGE(type() == nullValue || type() == arrayValue,
11311135
"in Json::Value::insert: requires arrayValue");
11321136
ArrayIndex length = size();

src/test_lib_json/main.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,7 @@ JSONTEST_FIXTURE_LOCAL(ValueTest, arrayInsertAtRandomIndex) {
417417
}
418418
vec.push_back(&array[4]);
419419
// insert rvalue at the tail
420-
Json::Value index5("index5");
421-
JSONTEST_ASSERT(array.insert(5, std::move(index5)));
420+
JSONTEST_ASSERT(array.insert(5, "index5"));
422421
JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[0]);
423422
JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[1]);
424423
JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[2]);

0 commit comments

Comments
 (0)