insert and remove methods
authorSean Middleditch <sean@middleditch.us>
Wed, 13 Jan 2010 00:17:11 +0000 (16:17 -0800)
committerSean Middleditch <sean@middleditch.us>
Wed, 13 Jan 2010 00:17:11 +0000 (16:17 -0800)
janssonxx.h
test.cc

index 6039646..0871c52 100644 (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:
diff --git a/test.cc b/test.cc
index ce9d761..055d4a9 100644 (file)
--- a/test.cc
+++ b/test.cc
@@ -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");