insert and remove methods
This commit is contained in:
parent
492d95329a
commit
17805e5829
29
janssonxx.h
29
janssonxx.h
@ -70,7 +70,12 @@ public:
|
|||||||
static Value from(const char* value) { return Value::_take(json_string(value)); }
|
static Value from(const char* value) { return Value::_take(json_string(value)); }
|
||||||
static Value from(const std::string& value) { return from(value.c_str()); }
|
static Value from(const std::string& value) { return from(value.c_str()); }
|
||||||
static Value from(bool value) { return Value::_take(value ? json_true() : json_false()); }
|
static Value from(bool value) { return Value::_take(value ? json_true() : json_false()); }
|
||||||
static Value from(int value) { return Value::_take(json_integer(value)); }
|
static Value from(signed int value) { return Value::_take(json_integer(value)); }
|
||||||
|
static Value from(unsigned int value) { return Value::_take(json_integer(value)); }
|
||||||
|
static Value from(signed short value) { return Value::_take(json_integer(value)); }
|
||||||
|
static Value from(unsigned short value) { return Value::_take(json_integer(value)); }
|
||||||
|
static Value from(signed long value) { return Value::_take(json_integer(value)); }
|
||||||
|
static Value from(unsigned long value) { return Value::_take(json_integer(value)); }
|
||||||
static Value from(double value) { return Value::_take(json_real(value)); }
|
static Value from(double value) { return Value::_take(json_real(value)); }
|
||||||
|
|
||||||
// create a new empty object
|
// create a new empty object
|
||||||
@ -174,8 +179,26 @@ public:
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Value& set_at(int index, const Value& value) {
|
// delete an object key
|
||||||
return set_at(static_cast<unsigned int>(index), value);
|
Value& del_key(const char* key) {
|
||||||
|
json_object_del(_value, key);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Value& del_key(const std::string& key) {
|
||||||
|
return del_key(key.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
// delete an item from an array by index
|
||||||
|
Value& del_at(unsigned int index) {
|
||||||
|
json_array_remove(_value, index);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// insert an item into an array at a given index
|
||||||
|
Value& insert_at(unsigned int index, const Value& value) {
|
||||||
|
json_array_insert(_value, index, value.as_json());
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
9
test.cc
9
test.cc
@ -90,6 +90,15 @@ int main() {
|
|||||||
ASSERT_TRUE(e10.is_array(), "e10 is not an array after index assignment");
|
ASSERT_TRUE(e10.is_array(), "e10 is not an array after index assignment");
|
||||||
ASSERT_EQ(e10.size(), 2, "e10 has incorrect number of elements after assignment");
|
ASSERT_EQ(e10.size(), 2, "e10 has incorrect number of elements after assignment");
|
||||||
|
|
||||||
|
e10.insert_at(1, jansson::Value::from("new"));
|
||||||
|
ASSERT_EQ(e10.size(), 3, "e10 has incorrect size after insert");
|
||||||
|
ASSERT_EQ(e10[1].as_string(), "new", "e10[1] has incorrect value after insert");
|
||||||
|
ASSERT_EQ(e10[2].as_string(), "foobar", "e10[2] has incorrect value after insert");
|
||||||
|
|
||||||
|
e10.del_at(0);
|
||||||
|
ASSERT_EQ(e10.size(), 2, "e10 has incorrect size after delete");
|
||||||
|
ASSERT_EQ(e10[1].as_string(), "foobar", "e10[1] has incorrect value after delete");
|
||||||
|
|
||||||
e10.clear();
|
e10.clear();
|
||||||
ASSERT_EQ(e10.size(), 0, "e10 has incorrect number of elements after clear");
|
ASSERT_EQ(e10.size(), 0, "e10 has incorrect number of elements after clear");
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user