From e0806677296e1d223adb02f0cbf0a03837bf893b Mon Sep 17 00:00:00 2001 From: Sean Middleditch Date: Mon, 18 Jan 2010 19:24:25 -0800 Subject: [PATCH 1/1] replace json::from() with explicit Value() constructors --- jansson-impl.hpp | 90 ++++++++++++++++++++++++++++---------------------------- jansson.hpp | 30 +++++++++---------- test.cpp | 30 +++++++++---------- 3 files changed, 75 insertions(+), 75 deletions(-) diff --git a/jansson-impl.hpp b/jansson-impl.hpp index 6242ed9..89242fa 100644 --- a/jansson-impl.hpp +++ b/jansson-impl.hpp @@ -328,6 +328,51 @@ namespace json { } } // 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 Iterator::Iterator(const Value& value) : _object(value), _iter(0) { @@ -375,51 +420,6 @@ namespace json { 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 Value object() { return Value::take_ownership(json_object()); diff --git a/jansson.hpp b/jansson.hpp index fc81d98..d3d11b3 100644 --- a/jansson.hpp +++ b/jansson.hpp @@ -151,9 +151,9 @@ namespace json { inline json_t* as_json() const; // 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 json_t* _value; }; @@ -203,6 +203,19 @@ namespace json { // represents any JSON value class Value : public _private::ValueBase<_private::Basic> { 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 Value() : _private::ValueBase<_private::Basic>() {} @@ -261,19 +274,6 @@ namespace json { 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 inline Value object(); diff --git a/test.cpp b/test.cpp index cfadd1c..d69c344 100644 --- a/test.cpp +++ b/test.cpp @@ -49,15 +49,15 @@ int json_cpp_tests() { i.next(); 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_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_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_EQ(e7.as_string(), "foobar", "e7 has incorrect value after assignment"); @@ -70,16 +70,16 @@ int json_cpp_tests() { json::Value e10(json::array()); 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[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_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"); - 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_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"); @@ -88,7 +88,7 @@ int json_cpp_tests() { 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, json::from("new")); + e10.insert_at(1, json::Value("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"); @@ -103,16 +103,16 @@ int json_cpp_tests() { json::Value e11(json::object()); 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["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_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"); - 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_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"); @@ -121,8 +121,8 @@ int json_cpp_tests() { ASSERT_EQ(e11.size(), 0, "e11 has incorrect number of properties after clear"); json::Value e12(json::object()); - e12.set_key("foo", json::from("test")); - e12.set_key("bar", json::from(3)); + e12.set_key("foo", json::Value("test")); + e12.set_key("bar", json::Value(3)); char* out_cstr = e12.save_string(0); std::string out(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"); e14.set_key("foo", json::object()); 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"); json::Value e15(json::array()); 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"); - e15[0] = json::from("foo"); + e15[0] = json::Value("foo"); ASSERT_EQ(e15[0].as_string(), "foo", "e15[0] has incorrecy value after assignment"); return 0; } -- 2.1.4