remove auto type conversion on array/object assignment

This commit is contained in:
Sean Middleditch 2010-01-12 15:38:47 -08:00
parent 8d3a9e347c
commit 7d09af38c1
2 changed files with 6 additions and 14 deletions

View File

@ -149,13 +149,7 @@ public:
// set an object property (converts value to object is not one already) // set an object property (converts value to object is not one already)
Value& set_key(const char* key, const Value& value) { Value& set_key(const char* key, const Value& value) {
if (!is_object()) {
json_decref(_value);
_value = json_object();
}
json_object_set(_value, key, value.as_json()); json_object_set(_value, key, value.as_json());
return *this; return *this;
} }
@ -165,16 +159,10 @@ public:
// set an array index (converts value to object is not one already) // set an array index (converts value to object is not one already)
Value& set_at(unsigned int index, const Value& value) { Value& set_at(unsigned int index, const Value& value) {
if (!is_array()) {
json_decref(_value);
_value = json_array();
}
if (index == size()) if (index == size())
json_array_append(_value, value.as_json()); json_array_append(_value, value.as_json());
else else
json_array_set(_value, index, value.as_json()); json_array_set(_value, index, value.as_json());
return *this; return *this;
} }

View File

@ -69,8 +69,10 @@ int main() {
e3 = jansson::Value::null(); e3 = jansson::Value::null();
ASSERT_TRUE(e3.is_null(), "e3 is not null after assignment"); ASSERT_TRUE(e3.is_null(), "e3 is not null after assignment");
e3.set_at(0, jansson::Value::from("foobar")); e3 = jansson::Value::array();
ASSERT_TRUE(e3.is_array(), "e3 is not an array after index assignment"); ASSERT_TRUE(e3.is_array(), "e3 is not an array after index assignment");
e3.set_at(0, jansson::Value::from("foobar"));
ASSERT_EQ(e3.size(), 1, "e3 has incorrect number of elements after assignment"); ASSERT_EQ(e3.size(), 1, "e3 has incorrect number of elements after assignment");
ASSERT_EQ(e3[0].as_string(), "foobar", "e3[0] has incorrect value after assignment"); ASSERT_EQ(e3[0].as_string(), "foobar", "e3[0] has incorrect value after assignment");
@ -91,8 +93,10 @@ int main() {
e3.clear(); e3.clear();
ASSERT_EQ(e3.size(), 0, "e3 has incorrect number of elements after clear"); ASSERT_EQ(e3.size(), 0, "e3 has incorrect number of elements after clear");
e3.set_key("foo", jansson::Value::from("test")); e3 = jansson::Value::object();
ASSERT_TRUE(e3.is_object(), "e3 is not an object after property assignment"); ASSERT_TRUE(e3.is_object(), "e3 is not an object after property assignment");
e3.set_key("foo", jansson::Value::from("test"));
ASSERT_EQ(e3.size(), 1, "e3 has incorrect number of properties after assignment"); ASSERT_EQ(e3.size(), 1, "e3 has incorrect number of properties after assignment");
ASSERT_EQ(e3["foo"].as_string(), "test", "e3.foo has incorrect value after assignment"); ASSERT_EQ(e3["foo"].as_string(), "test", "e3.foo has incorrect value after assignment");