move static functions out of Value, add test driver to ensure linking works properly

This commit is contained in:
Sean Middleditch 2010-01-18 18:50:13 -08:00
parent 95bf762eeb
commit ef6c35ae1b
5 changed files with 138 additions and 134 deletions

View File

@ -3,8 +3,8 @@ JANSSON_LIBS := $(shell pkg-config --libs jansson)
all: test all: test
test-bin: test.cpp jansson.hpp jansson-impl.hpp Makefile test-bin: driver.cpp test.cpp jansson.hpp jansson-impl.hpp Makefile
$(CXX) -o $@ -g -O0 -Wall $(JANSSON_CFLAGS) $< $(JANSSON_LIBS) $(CXX) -o $@ -g -O0 -Wall $(JANSSON_CFLAGS) driver.cpp test.cpp $(JANSSON_LIBS)
test: test-bin test: test-bin
./test-bin ./test-bin

7
driver.cpp Normal file
View File

@ -0,0 +1,7 @@
#include "jansson.hpp"
extern int json_cpp_tests();
int main() {
return json_cpp_tests();
}

View File

@ -300,7 +300,7 @@ namespace json {
} }
// take ownership of a json_t (does not increase reference count) // take ownership of a json_t (does not increase reference count)
Basic Basic::_take(json_t* json) { Basic Basic::take_ownership(json_t* json) {
Basic v; Basic v;
v._value = json; v._value = json;
return v; return v;
@ -329,76 +329,6 @@ namespace json {
} // namespace json::_private } // namespace json::_private
// construct Value from input
Value Value::from(const char* value) {
return Value::_take(json_string(value));
}
Value Value::from(const std::string& value) {
return Value::from(value.c_str());
}
Value Value::from(bool value) {
return Value::_take(value ? json_true() : json_false());
}
Value Value::from(signed int value) {
return Value::_take(json_integer(value));
}
Value Value::from(unsigned int value) {
return Value::_take(json_integer(value));
}
Value Value::from(signed short value) {
return Value::_take(json_integer(value));
}
Value Value::from(unsigned short value) {
return Value::_take(json_integer(value));
}
Value Value::from(signed long value) {
return Value::_take(json_integer(value));
}
Value Value::from(unsigned long value) {
return Value::_take(json_integer(value));
}
Value Value::from(float value) {
return Value::_take(json_real(value));
}
Value Value::from(double value) {
return Value::_take(json_real(value));
}
// create a new empty object
Value Value::object() {
return Value::_take(json_object());
}
// create a new empty array
Value Value::array() {
return Value::_take(json_array());
}
// create a new null value
Value Value::null() {
return Value::_take(json_null());
}
// load a file as a JSON value
Value Value::load_file(const char* path, json_error_t* error) {
return Value::_take(json_load_file(path, error));
}
// load a string as a JSON value
Value Value::load_string(const char* string, json_error_t* error) {
return Value::_take(json_loads(string, error));
}
// 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());
@ -445,6 +375,76 @@ 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
Value object() {
return Value::take_ownership(json_object());
}
// create a new empty array
Value array() {
return Value::take_ownership(json_array());
}
// create a new null value
Value null() {
return Value::take_ownership(json_null());
}
// load a file as a JSON value
Value load_file(const char* path, json_error_t* error) {
return Value::take_ownership(json_load_file(path, error));
}
// load a string as a JSON value
Value load_string(const char* string, json_error_t* error) {
return Value::take_ownership(json_loads(string, error));
}
} // namespace json } // namespace json
// stream JSON value out // stream JSON value out
@ -466,6 +466,6 @@ std::istream& operator>>(std::istream& is, json::Value& value) {
while (is) while (is)
tmp << static_cast<char>(is.get()); tmp << static_cast<char>(is.get());
// parse the buffered string // parse the buffered string
value = json::Value::load_string(tmp.str().c_str()); value = json::load_string(tmp.str().c_str());
return is; return is;
} }

View File

@ -150,9 +150,8 @@ namespace json {
// get the underlying json_t // get the underlying json_t
inline json_t* as_json() const; inline json_t* as_json() const;
protected:
// 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(json_t* json); static inline Basic take_ownership(json_t* json);
private: private:
// internal value pointer // internal value pointer
@ -218,34 +217,6 @@ namespace json {
// create reference to value // create reference to value
explicit Value(json_t* json) : _private::ValueBase<_private::Basic>(json) {} explicit Value(json_t* json) : _private::ValueBase<_private::Basic>(json) {}
// construct Value from input
static inline Value from(const char* value);
static inline Value from(const std::string& value);
static inline Value from(bool value);
static inline Value from(signed int value);
static inline Value from(unsigned int value);
static inline Value from(signed short value);
static inline Value from(unsigned short value);
static inline Value from(signed long value);
static inline Value from(unsigned long value);
static inline Value from(float value);
static inline Value from(double value);
// create a new empty object
static inline Value object();
// create a new empty array
static inline Value array();
// create a new null value
static inline Value null();
// load a file as a JSON value
static inline Value load_file(const char* path, json_error_t* error = 0);
// load a string as a JSON value
static inline Value load_string(const char* string, json_error_t* error = 0);
}; };
// iterators over a JSON object // iterators over a JSON object
@ -290,6 +261,34 @@ 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
inline Value object();
// create a new empty array
inline Value array();
// create a new null value
inline Value null();
// load a file as a JSON value
inline Value load_file(const char* path, json_error_t* error = 0);
// load a string as a JSON value
inline Value load_string(const char* string, json_error_t* error = 0);
} // namespace json } // namespace json
// stream JSON value out -- inefficient and not recommended for production use // stream JSON value out -- inefficient and not recommended for production use

View File

@ -4,8 +4,6 @@
#include "jansson.hpp" #include "jansson.hpp"
using namespace std;
#define ASSERT_OP(lhs, rhs, op, m) \ #define ASSERT_OP(lhs, rhs, op, m) \
do { \ do { \
if(!((lhs) op (rhs))) { \ if(!((lhs) op (rhs))) { \
@ -21,11 +19,11 @@ using namespace std;
#define ASSERT_TRUE(p, m) ASSERT_OP(p, true, ==, m) #define ASSERT_TRUE(p, m) ASSERT_OP(p, true, ==, m)
#define ASSERT_FALSE(p, m) ASSERT_OP(p, true, !=, m) #define ASSERT_FALSE(p, m) ASSERT_OP(p, true, !=, m)
int main() { int json_cpp_tests() {
json::Value e1(json::Value::load_file("test.json")); json::Value e1(json::load_file("test.json"));
json::Value e2(e1); json::Value e2(e1);
json::Value e3; json::Value e3;
json::Value e4(json::Value::load_string("{\"foo\": true, \"bar\": \"test\"}")); json::Value e4(json::load_string("{\"foo\": true, \"bar\": \"test\"}"));
ASSERT_TRUE(e1.is_object(), "e1 is not an object"); ASSERT_TRUE(e1.is_object(), "e1 is not an object");
ASSERT_TRUE(e2.is_object(), "e2 is not an object"); ASSERT_TRUE(e2.is_object(), "e2 is not an object");
@ -51,46 +49,46 @@ int main() {
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::Value::from(12.34)); json::Value e5(json::from(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::Value::from(true)); json::Value e6(json::from(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::Value::from("foobar")); json::Value e7(json::from("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");
json::Value e8(json::Value::object()); json::Value e8(json::object());
ASSERT_TRUE(e8.is_object(), "e8 is not an object after assignment"); ASSERT_TRUE(e8.is_object(), "e8 is not an object after assignment");
json::Value e9(json::Value::null()); json::Value e9(json::null());
ASSERT_TRUE(e9.is_null(), "e9 is not null after assignment"); ASSERT_TRUE(e9.is_null(), "e9 is not null after assignment");
json::Value e10(json::Value::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::Value::from("foobar")); e10.set_at(0, json::from("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::Value::from("foobar")); e10.set_at(1, json::from("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::Value::from("barfoo")); e10.set_at(0, json::from("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");
e10.set_at(100, json::Value::null()); e10.set_at(100, json::null());
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::Value::from("new")); e10.insert_at(1, json::from("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");
@ -102,19 +100,19 @@ int main() {
e10.clear(); e10.clear();
ASSERT_EQ(e10.size(), 0, "e10 has incorrect number of elements after clear"); ASSERT_EQ(e10.size(), 0, "e10 has incorrect number of elements after clear");
json::Value e11(json::Value::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::Value::from("test")); e11.set_key("foo", json::from("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::Value::from("again")); e11.set_key("foo", json::from("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::Value::from("test")); e11.set_key("bar", json::from("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");
@ -122,11 +120,11 @@ int main() {
e11.clear(); e11.clear();
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::Value::object()); json::Value e12(json::object());
e12.set_key("foo", json::Value::from("test")); e12.set_key("foo", json::from("test"));
e12.set_key("bar", json::Value::from(3)); e12.set_key("bar", json::from(3));
char* out_cstr = e12.save_string(0); char* out_cstr = e12.save_string(0);
string out(out_cstr); std::string out(out_cstr);
free(out_cstr); free(out_cstr);
ASSERT_EQ(out, "{\"bar\": 3,\"foo\": \"test\"}\n", "object did not serialize as expected"); ASSERT_EQ(out, "{\"bar\": 3,\"foo\": \"test\"}\n", "object did not serialize as expected");
@ -144,18 +142,18 @@ int main() {
const json::Value e13(e12); const json::Value e13(e12);
ASSERT_EQ(e13["bar"].as_integer(), 3, "e13.bar has incorrect value after copy"); ASSERT_EQ(e13["bar"].as_integer(), 3, "e13.bar has incorrect value after copy");
json::Value e14(json::Value::object()); json::Value e14(json::object());
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::Value::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::Value::from(42); e14["foo"]["bar"] = json::from(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::Value::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::Value::from(42)); e15.set_at(0, json::from(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::Value::from("foo"); e15[0] = json::from("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;
} }