insert and remove methods

This commit is contained in:
Sean Middleditch 2010-01-12 16:17:11 -08:00
parent 492d95329a
commit 17805e5829
2 changed files with 35 additions and 3 deletions

View File

@ -70,7 +70,12 @@ public:
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(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)); }
// create a new empty object
@ -174,8 +179,26 @@ public:
return *this;
}
Value& set_at(int index, const Value& value) {
return set_at(static_cast<unsigned int>(index), value);
// delete an object key
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:

View File

@ -90,6 +90,15 @@ int main() {
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");
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();
ASSERT_EQ(e10.size(), 0, "e10 has incorrect number of elements after clear");