all: test
-test-bin: test.cpp jansson.hpp jansson-impl.hpp Makefile
- $(CXX) -o $@ -g -O0 -Wall $(JANSSON_CFLAGS) $< $(JANSSON_LIBS)
+test-bin: driver.cpp test.cpp jansson.hpp jansson-impl.hpp Makefile
+ $(CXX) -o $@ -g -O0 -Wall $(JANSSON_CFLAGS) driver.cpp test.cpp $(JANSSON_LIBS)
test: test-bin
./test-bin
--- /dev/null
+#include "jansson.hpp"
+
+extern int json_cpp_tests();
+
+int main() {
+ return json_cpp_tests();
+}
}
// 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;
v._value = json;
return v;
}
} // namespace json::_private
-
- // construct Value from input
- Value Value::from(const char* value) {
- return Value::_take(json_string(value));
+
+ // construct a new iterator for a given object
+ Iterator::Iterator(const Value& value) : _object(value), _iter(0) {
+ _iter = json_object_iter(_object.as_json());
}
- Value Value::from(const std::string& value) {
- return Value::from(value.c_str());
+ // construct a new iterator for a given object
+ Iterator::Iterator(const _private::ValueBase<_private::PropertyProxy>& value) :
+ _object(value.as_json()), _iter(0) {
+ _iter = json_object_iter(_object.as_json());
}
- Value Value::from(bool value) {
- return Value::_take(value ? json_true() : json_false());
+ // increment iterator
+ void Iterator::next() {
+ _iter = json_object_iter_next(_object.as_json(), _iter);
}
- Value Value::from(signed int value) {
- return Value::_take(json_integer(value));
- }
+ Iterator& Iterator::operator++() { next(); return *this; }
- Value Value::from(unsigned int value) {
- return Value::_take(json_integer(value));
+ // test if iterator is still valid
+ bool Iterator::valid() const {
+ return _iter != 0;
}
- Value Value::from(signed short value) {
- return Value::_take(json_integer(value));
+ Iterator::operator bool() const {
+ return valid();
}
- Value Value::from(unsigned short value) {
- return Value::_take(json_integer(value));
+ // get key
+ const char* Iterator::ckey() const {
+ return json_object_iter_key(_iter);
}
- Value Value::from(signed long value) {
- return Value::_take(json_integer(value));
+ std::string Iterator::key() const {
+ return ckey();
}
- Value Value::from(unsigned long value) {
- return Value::_take(json_integer(value));
+ // get value
+ const Value Iterator::value() const {
+ return Value(json_object_iter_value(_iter));
}
- Value Value::from(float value) {
- return Value::_take(json_real(value));
+ // dereference value
+ const Value Iterator::operator*() const {
+ return value();
}
- Value Value::from(double value) {
- return Value::_take(json_real(value));
+ // construct Value from input
+ Value from(const char* value) {
+ return Value::take_ownership(json_string(value));
}
- // create a new empty object
- Value Value::object() {
- return Value::_take(json_object());
+ Value from(const std::string& value) {
+ return from(value.c_str());
}
- // create a new empty array
- Value Value::array() {
- return Value::_take(json_array());
+ Value from(bool value) {
+ return Value::take_ownership(value ? json_true() : json_false());
}
- // create a new null value
- Value Value::null() {
- return Value::_take(json_null());
+ Value from(signed int value) {
+ return Value::take_ownership(json_integer(value));
}
- // 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));
+ Value from(unsigned int value) {
+ return Value::take_ownership(json_integer(value));
}
- // 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));
+ Value from(signed short value) {
+ return Value::take_ownership(json_integer(value));
}
-
- // construct a new iterator for a given object
- Iterator::Iterator(const Value& value) : _object(value), _iter(0) {
- _iter = json_object_iter(_object.as_json());
+
+ Value from(unsigned short value) {
+ return Value::take_ownership(json_integer(value));
}
- // construct a new iterator for a given object
- Iterator::Iterator(const _private::ValueBase<_private::PropertyProxy>& value) :
- _object(value.as_json()), _iter(0) {
- _iter = json_object_iter(_object.as_json());
+ Value from(signed long value) {
+ return Value::take_ownership(json_integer(value));
}
- // increment iterator
- void Iterator::next() {
- _iter = json_object_iter_next(_object.as_json(), _iter);
+ Value from(unsigned long value) {
+ return Value::take_ownership(json_integer(value));
}
- Iterator& Iterator::operator++() { next(); return *this; }
+ Value from(float value) {
+ return Value::take_ownership(json_real(value));
+ }
- // test if iterator is still valid
- bool Iterator::valid() const {
- return _iter != 0;
+ Value from(double value) {
+ return Value::take_ownership(json_real(value));
}
- Iterator::operator bool() const {
- return valid();
+ // create a new empty object
+ Value object() {
+ return Value::take_ownership(json_object());
}
- // get key
- const char* Iterator::ckey() const {
- return json_object_iter_key(_iter);
+ // create a new empty array
+ Value array() {
+ return Value::take_ownership(json_array());
}
- std::string Iterator::key() const {
- return ckey();
+ // create a new null value
+ Value null() {
+ return Value::take_ownership(json_null());
}
- // get value
- const Value Iterator::value() const {
- return Value(json_object_iter_value(_iter));
+ // 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));
}
- // dereference value
- const Value Iterator::operator*() const {
- return value();
+ // 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
while (is)
tmp << static_cast<char>(is.get());
// parse the buffered string
- value = json::Value::load_string(tmp.str().c_str());
+ value = json::load_string(tmp.str().c_str());
return is;
}
// get the underlying json_t
inline json_t* as_json() const;
- protected:
// 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:
// internal value pointer
// create reference to value
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
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
// stream JSON value out -- inefficient and not recommended for production use
#include "jansson.hpp"
-using namespace std;
-
#define ASSERT_OP(lhs, rhs, op, m) \
do { \
if(!((lhs) op (rhs))) { \
#define ASSERT_TRUE(p, m) ASSERT_OP(p, true, ==, m)
#define ASSERT_FALSE(p, m) ASSERT_OP(p, true, !=, m)
-int main() {
- json::Value e1(json::Value::load_file("test.json"));
+int json_cpp_tests() {
+ json::Value e1(json::load_file("test.json"));
json::Value e2(e1);
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(e2.is_object(), "e2 is not an object");
i.next();
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_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_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_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");
- json::Value e9(json::Value::null());
+ json::Value e9(json::null());
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");
- 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[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_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::Value::from("barfoo"));
+ e10.set_at(0, json::from("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");
- 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_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[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.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");
- 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["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_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::Value::from("test"));
+ e11.set_key("bar", json::from("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");
e11.clear();
ASSERT_EQ(e11.size(), 0, "e11 has incorrect number of properties after clear");
- json::Value e12(json::Value::object());
- e12.set_key("foo", json::Value::from("test"));
- e12.set_key("bar", json::Value::from(3));
+ json::Value e12(json::object());
+ e12.set_key("foo", json::from("test"));
+ e12.set_key("bar", json::from(3));
char* out_cstr = e12.save_string(0);
- string out(out_cstr);
+ std::string out(out_cstr);
free(out_cstr);
ASSERT_EQ(out, "{\"bar\": 3,\"foo\": \"test\"}\n", "object did not serialize as expected");
const json::Value e13(e12);
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");
- 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");
- 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");
- json::Value e15(json::Value::array());
+ json::Value e15(json::array());
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");
- 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");
return 0;
}