replace json::from() with explicit Value() constructors

This commit is contained in:
Sean Middleditch 2010-01-18 19:24:25 -08:00
parent ef6c35ae1b
commit e080667729
3 changed files with 75 additions and 75 deletions

View File

@ -329,6 +329,51 @@ namespace json {
} // namespace json::_private } // namespace json::_private
// construct Value::Value input
Value::Value(const char* value) {
_value = json_string(value);
}
Value::Value(const std::string& value) {
_value = json_string(value.c_str());
}
Value::Value(bool value) {
_value = value ? json_true() : json_false();
}
Value::Value(signed int value) {
_value = json_integer(value);
}
Value::Value(unsigned int value) {
_value = json_integer(value);
}
Value::Value(signed short value) {
_value = json_integer(value);
}
Value::Value(unsigned short value) {
_value = json_integer(value);
}
Value::Value(signed long value) {
_value = json_integer(value);
}
Value::Value(unsigned long value) {
_value = json_integer(value);
}
Value::Value(float value) {
_value = json_real(value);
}
Value::Value(double value) {
_value = json_real(value);
}
// construct a new iterator for a given object // construct a new iterator for a given object
Iterator::Iterator(const Value& value) : _object(value), _iter(0) { Iterator::Iterator(const Value& value) : _object(value), _iter(0) {
_iter = json_object_iter(_object.as_json()); _iter = json_object_iter(_object.as_json());
@ -375,51 +420,6 @@ namespace json {
return value(); return value();
} }
// construct Value from input
Value from(const char* value) {
return Value::take_ownership(json_string(value));
}
Value from(const std::string& value) {
return from(value.c_str());
}
Value from(bool value) {
return Value::take_ownership(value ? json_true() : json_false());
}
Value from(signed int value) {
return Value::take_ownership(json_integer(value));
}
Value from(unsigned int value) {
return Value::take_ownership(json_integer(value));
}
Value from(signed short value) {
return Value::take_ownership(json_integer(value));
}
Value from(unsigned short value) {
return Value::take_ownership(json_integer(value));
}
Value from(signed long value) {
return Value::take_ownership(json_integer(value));
}
Value from(unsigned long value) {
return Value::take_ownership(json_integer(value));
}
Value from(float value) {
return Value::take_ownership(json_real(value));
}
Value from(double value) {
return Value::take_ownership(json_real(value));
}
// create a new empty object // create a new empty object
Value object() { Value object() {
return Value::take_ownership(json_object()); return Value::take_ownership(json_object());

View File

@ -151,9 +151,9 @@ namespace json {
inline json_t* as_json() const; inline json_t* as_json() const;
// take ownership of a json_t (does not increase reference count) // take ownership of a json_t (does not increase reference count)
static inline Basic take_ownership(json_t* json); inline static Basic take_ownership(json_t* json);
private: protected:
// internal value pointer // internal value pointer
json_t* _value; json_t* _value;
}; };
@ -203,6 +203,19 @@ namespace json {
// represents any JSON value // represents any JSON value
class Value : public _private::ValueBase<_private::Basic> { class Value : public _private::ValueBase<_private::Basic> {
public: public:
// construct Value from input
explicit inline Value(const char* value);
explicit inline Value(const std::string& value);
explicit inline Value(bool value);
explicit inline Value(signed int value);
explicit inline Value(unsigned int value);
explicit inline Value(signed short value);
explicit inline Value(unsigned short value);
explicit inline Value(signed long value);
explicit inline Value(unsigned long value);
explicit inline Value(float value);
explicit inline Value(double value);
// empty constructor // empty constructor
Value() : _private::ValueBase<_private::Basic>() {} Value() : _private::ValueBase<_private::Basic>() {}
@ -261,19 +274,6 @@ namespace json {
void* _iter; void* _iter;
}; };
// construct Value from input
inline Value from(const char* value);
inline Value from(const std::string& value);
inline Value from(bool value);
inline Value from(signed int value);
inline Value from(unsigned int value);
inline Value from(signed short value);
inline Value from(unsigned short value);
inline Value from(signed long value);
inline Value from(unsigned long value);
inline Value from(float value);
inline Value from(double value);
// create a new empty object // create a new empty object
inline Value object(); inline Value object();

View File

@ -49,15 +49,15 @@ int json_cpp_tests() {
i.next(); i.next();
ASSERT_FALSE(i.valid(), "iterator has more values than expected"); ASSERT_FALSE(i.valid(), "iterator has more values than expected");
json::Value e5(json::from(12.34)); json::Value e5(json::Value(12.34));
ASSERT_TRUE(e5.is_number(), "e5 is not a number after assignment"); ASSERT_TRUE(e5.is_number(), "e5 is not a number after assignment");
ASSERT_EQ(e5.as_real(), 12.34, "e5 has incorrect value after assignment"); ASSERT_EQ(e5.as_real(), 12.34, "e5 has incorrect value after assignment");
json::Value e6(json::from(true)); json::Value e6(json::Value(true));
ASSERT_TRUE(e6.is_boolean(), "e6 is not a boolean after assignment"); ASSERT_TRUE(e6.is_boolean(), "e6 is not a boolean after assignment");
ASSERT_EQ(e6.as_boolean(), true, "e6 has incorrect value after assignment"); ASSERT_EQ(e6.as_boolean(), true, "e6 has incorrect value after assignment");
json::Value e7(json::from("foobar")); json::Value e7(json::Value("foobar"));
ASSERT_TRUE(e7.is_string(), "e7 is not a string after assignment"); ASSERT_TRUE(e7.is_string(), "e7 is not a string after assignment");
ASSERT_EQ(e7.as_string(), "foobar", "e7 has incorrect value after assignment"); ASSERT_EQ(e7.as_string(), "foobar", "e7 has incorrect value after assignment");
@ -70,16 +70,16 @@ int json_cpp_tests() {
json::Value e10(json::array()); json::Value e10(json::array());
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");
e10.set_at(0, json::from("foobar")); e10.set_at(0, json::Value("foobar"));
ASSERT_EQ(e10.size(), 1, "e10 has incorrect number of elements after assignment"); ASSERT_EQ(e10.size(), 1, "e10 has incorrect number of elements after assignment");
ASSERT_EQ(e10[0].as_string(), "foobar", "e10[0] has incorrect value after assignment"); ASSERT_EQ(e10[0].as_string(), "foobar", "e10[0] has incorrect value after assignment");
e10.set_at(1, json::from("foobar")); e10.set_at(1, json::Value("foobar"));
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");
ASSERT_EQ(e10[1].as_string(), "foobar", "e10[0] has incorrect value after assignment"); ASSERT_EQ(e10[1].as_string(), "foobar", "e10[0] has incorrect value after assignment");
e10.set_at(0, json::from("barfoo")); e10.set_at(0, json::Value("barfoo"));
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");
ASSERT_EQ(e10[0].as_string(), "barfoo", "e10[0] has incorrect value after assignment"); ASSERT_EQ(e10[0].as_string(), "barfoo", "e10[0] has incorrect value after assignment");
@ -88,7 +88,7 @@ int json_cpp_tests() {
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, json::from("new")); e10.insert_at(1, json::Value("new"));
ASSERT_EQ(e10.size(), 3, "e10 has incorrect size after insert"); 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[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"); ASSERT_EQ(e10[2].as_string(), "foobar", "e10[2] has incorrect value after insert");
@ -103,16 +103,16 @@ int json_cpp_tests() {
json::Value e11(json::object()); json::Value e11(json::object());
ASSERT_TRUE(e11.is_object(), "e11 is not an object after property assignment"); ASSERT_TRUE(e11.is_object(), "e11 is not an object after property assignment");
e11.set_key("foo", json::from("test")); e11.set_key("foo", json::Value("test"));
ASSERT_EQ(e11.size(), 1, "e11 has incorrect number of properties after assignment"); ASSERT_EQ(e11.size(), 1, "e11 has incorrect number of properties after assignment");
ASSERT_EQ(e11["foo"].as_string(), "test", "e11.foo has incorrect value after assignment"); ASSERT_EQ(e11["foo"].as_string(), "test", "e11.foo has incorrect value after assignment");
e11.set_key("foo", json::from("again")); e11.set_key("foo", json::Value("again"));
ASSERT_TRUE(e11.is_object(), "e11 is not an object after property assignment"); ASSERT_TRUE(e11.is_object(), "e11 is not an object after property assignment");
ASSERT_EQ(e11.size(), 1, "e11 has incorrect number of properties after assignment"); ASSERT_EQ(e11.size(), 1, "e11 has incorrect number of properties after assignment");
ASSERT_EQ(e11["foo"].as_string(), "again", "e11.foo has incorrect value after assignment"); ASSERT_EQ(e11["foo"].as_string(), "again", "e11.foo has incorrect value after assignment");
e11.set_key("bar", json::from("test")); e11.set_key("bar", json::Value("test"));
ASSERT_TRUE(e11.is_object(), "e11 is not an object after property assignment"); ASSERT_TRUE(e11.is_object(), "e11 is not an object after property assignment");
ASSERT_EQ(e11.size(), 2, "e11 has incorrect number of properties after assignment"); ASSERT_EQ(e11.size(), 2, "e11 has incorrect number of properties after assignment");
ASSERT_EQ(e11["bar"].as_string(), "test", "e11.foo has incorrect value after assignment"); ASSERT_EQ(e11["bar"].as_string(), "test", "e11.foo has incorrect value after assignment");
@ -121,8 +121,8 @@ int json_cpp_tests() {
ASSERT_EQ(e11.size(), 0, "e11 has incorrect number of properties after clear"); ASSERT_EQ(e11.size(), 0, "e11 has incorrect number of properties after clear");
json::Value e12(json::object()); json::Value e12(json::object());
e12.set_key("foo", json::from("test")); e12.set_key("foo", json::Value("test"));
e12.set_key("bar", json::from(3)); e12.set_key("bar", json::Value(3));
char* out_cstr = e12.save_string(0); char* out_cstr = e12.save_string(0);
std::string out(out_cstr); std::string out(out_cstr);
free(out_cstr); free(out_cstr);
@ -146,14 +146,14 @@ int json_cpp_tests() {
ASSERT_TRUE(e14.is_object(), "e14 is not an object after construction"); ASSERT_TRUE(e14.is_object(), "e14 is not an object after construction");
e14.set_key("foo", json::object()); e14.set_key("foo", json::object());
ASSERT_TRUE(e14["foo"].is_object(), "e14.foo is not an object after assignment"); ASSERT_TRUE(e14["foo"].is_object(), "e14.foo is not an object after assignment");
e14["foo"]["bar"] = json::from(42); e14["foo"]["bar"] = json::Value(42);
ASSERT_EQ(e14["foo"]["bar"].as_integer(), 42, "e14.foo.bar has incorrect value after assignment"); ASSERT_EQ(e14["foo"]["bar"].as_integer(), 42, "e14.foo.bar has incorrect value after assignment");
json::Value e15(json::array()); json::Value e15(json::array());
ASSERT_TRUE(e15.is_array(), "e15 is not an array after construction"); ASSERT_TRUE(e15.is_array(), "e15 is not an array after construction");
e15.set_at(0, json::from(42)); e15.set_at(0, json::Value(42));
ASSERT_EQ(e15[0].as_integer(), 42, "e15[0] has incorrect value after assignment"); ASSERT_EQ(e15[0].as_integer(), 42, "e15[0] has incorrect value after assignment");
e15[0] = json::from("foo"); e15[0] = json::Value("foo");
ASSERT_EQ(e15[0].as_string(), "foo", "e15[0] has incorrecy value after assignment"); ASSERT_EQ(e15[0].as_string(), "foo", "e15[0] has incorrecy value after assignment");
return 0; return 0;
} }