Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/json/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,12 @@ Json::Value obj_value(Json::objectValue); // {}
///
/// Equivalent to jsonvalue[jsonvalue.size()] = value;
Value& append(const Value& value);
/// \brief Insert value in array at specific index
bool insert(ArrayIndex index, const Value& newValue);
#if JSON_HAS_RVALUE_REFERENCES
Value& append(Value&& value);
bool insert(ArrayIndex index, const Value&& newValue);
#endif

/// Access an object value by name, create a null member if it does not exist.
/// \note Because of our implementation, keys are limited to 2^30 -1 chars.
Expand Down
45 changes: 41 additions & 4 deletions src/lib_json/json_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1170,11 +1170,48 @@ Value const& Value::operator[](CppTL::ConstString const& key) const {
#endif

Value& Value::append(const Value& value) { return (*this)[size()] = value; }

Value& Value::append(Value&& value) {
return (*this)[size()] = std::move(value);
/// \brief Insert value in array at specific index
bool Value::insert(ArrayIndex index, const Value& newValue) {
if(type() != arrayValue) {
return false;
}
if(!isValidIndex(index)) {
return false;
}
ArrayIndex oldsize = size();
resize(oldsize+1);
ArrayIndex length = size();
if(length != oldsize +1) {
return false;
}
for(ArrayIndex i = length ; i> index ; i--) {
(*this)[i] = (*this)[i-1];
}
(*this)[index]= newValue;
return true;
}

#if JSON_HAS_RVALUE_REFERENCES
Value& Value::append(Value&& value) { return (*this)[size()] = std::move(value); }
bool Value::insert(ArrayIndex index, const Value&& newValue) {
if(type() != arrayValue) {
return false;
}
if(!isValidIndex(index)) {
return false;
}
ArrayIndex oldsize = size();
resize(oldsize+1);
ArrayIndex length = size();
if(length != oldsize +1) {
return false;
}
for(ArrayIndex i = length ; i> index ; i--) {
(*this)[i] = (*this)[i-1];
}
(*this)[index]= std::move(newValue);
return true;
}
#endif
Value Value::get(char const* begin,
char const* end,
Value const& defaultValue) const {
Expand Down
15 changes: 15 additions & 0 deletions src/test_lib_json/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,21 @@ JSONTEST_FIXTURE(ValueTest, arrayIssue252) {
}
// JSONTEST_ASSERT_EQUAL(5, root["array"].size());
}
JSONTEST_FIXTURE(ValueTest, arrayIssue691) {
Json::Value array2;
array2.append(10); // index 0
array2.append(20); // index 1
array2.append(30); // index 2
array2.append(50); // index 3

JSONTEST_ASSERT_EQUAL(Json::Value(50),array2[3]);

array2.insert(3,40); // index 3
// After updating, index 3 should be changed from 50 to 40, and 50 moved to
// index 4.
JSONTEST_ASSERT_EQUAL(Json::Value(40),array2[3]);
JSONTEST_ASSERT_EQUAL(Json::Value(50),array2[4]);
}
JSONTEST_FIXTURE(ValueTest, null) {
JSONTEST_ASSERT_EQUAL(Json::nullValue, null_.type());

Expand Down Expand Up @@ -2535,6 +2549,7 @@ int main(int argc, const char* argv[]) {
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, objects);
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, arrays);
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, arrayIssue252);
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, arrayIssue691);
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, null);
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, strings);
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, bools);
Expand Down