templatize janssonxx functionality to prepare for proxy setters

This commit is contained in:
Sean Middleditch 2010-01-16 01:13:19 -08:00
parent 69437a7183
commit 87df8bb0fe
3 changed files with 350 additions and 212 deletions

View File

@ -16,29 +16,116 @@
#include <cstdlib> #include <cstdlib>
namespace jansson { namespace jansson {
// include in the jansson namespace // include in the jansson namespace
# include <jansson.h> # include <jansson.h>
class Iterator; class Iterator;
class Value;
// represents any JSON value // base class for JSON value interface
class Value { template <typename _Base>
class _ValueBase : public _Base {
public:
// empty constructor
_ValueBase() : _Base() {}
// copy constructor
_ValueBase(const _Base& base) : _Base(base) {}
// create reference to value
_ValueBase(json_t* json) : _Base(json) {}
// check value type
bool is_undefined() const { return _Base::as_json() == 0; }
bool is_object() const { return json_is_object(_Base::as_json()); }
bool is_array() const { return json_is_array(_Base::as_json()); }
bool is_string() const { return json_is_string(_Base::as_json()); }
bool is_integer() const { return json_is_integer(_Base::as_json()); }
bool is_real() const { return json_is_real(_Base::as_json()); }
bool is_number() const { return json_is_number(_Base::as_json()); }
bool is_true() const { return json_is_true(_Base::as_json()); }
bool is_false() const { return json_is_false(_Base::as_json()); }
bool is_boolean() const { return json_is_boolean(_Base::as_json()); }
bool is_null() const { return json_is_null(_Base::as_json()); }
// get size of array or object
inline unsigned int size() const;
// get value at array index (const version)
inline const Value at(unsigned int index) const;
inline const Value operator[](signed int index) const;
inline const Value operator[](unsigned int index) const;
inline const Value operator[](signed short index) const;
inline const Value operator[](unsigned short index) const;
inline const Value operator[](signed long index) const;
inline const Value operator[](unsigned long index) const;
// get value at array index (non-const version)
inline Value at(unsigned int index);
inline Value operator[](signed int index);
inline Value operator[](unsigned int index);
inline Value operator[](signed short index);
inline Value operator[](unsigned short index);
inline Value operator[](signed long index);
inline Value operator[](unsigned long index);
// get object property
inline const Value get(const char* key) const;
inline const Value get(const std::string& key) const;
inline const Value operator[](const char* key) const;
inline const Value operator[](const std::string& key) const;
// clear all array/object values
inline void clear();
// get value cast to specified type
inline const char* as_cstring() const;
inline std::string as_string() const;
inline int as_integer() const;
inline double as_real() const;
inline double as_number() const;
inline bool as_boolean() const;
// set an object property (converts value to object is not one already)
inline _Base& set_key(const char* key, const Value& value);
inline _Base& set_key(const std::string& key, const Value& value);
// set an array index (converts value to object is not one already)
inline _Base& set_at(unsigned int index, const Value& value);
// delete an object key
inline _Base& del_key(const char* key);
inline _Base& del_key(const std::string& key);
// delete an item from an array by index
inline _Base& del_at(unsigned int index);
// insert an item into an array at a given index
inline _Base& insert_at(unsigned int index, const Value& value);
};
// represents any JSON value, private base
class _Value {
public: public:
// construct new Value with an undefined value // construct new Value with an undefined value
Value() : _value(0) {} _Value() : _value(0) {}
// free Value resources // copy constructor
~Value() { json_decref(_value); } _Value(const _Value& value) : _value(json_incref(value._value)) {}
// copy an existing Value
Value(const Value& e) : _value(json_incref(e._value)) {}
// make a reference to an existing json_t value // make a reference to an existing json_t value
explicit Value(json_t* value) : _value(json_incref(value)) {} explicit _Value(json_t* value) : _value(json_incref(value)) {}
// free Value resources
~_Value() { json_decref(_value); }
// copy an existing Value // copy an existing Value
Value& operator=(const Value& e) { _Value& operator=(const _Value& e) {
if (&e != this) { if (&e != this) {
json_decref(_value); json_decref(_value);
_value = json_incref(e._value); _value = json_incref(e._value);
@ -46,6 +133,62 @@ public:
return *this; return *this;
} }
// get the underlying json_t
json_t* as_json() const { return _value; }
protected:
// take ownership of a json_t (does not increase reference count)
static _Value _take(json_t* json) {
_Value v;
v._value = json;
return v;
}
private:
// internal value pointer
json_t* _value;
};
// represents any JSON value
class Value : public _ValueBase<_Value> {
public:
// empty constructor
Value() : _ValueBase<_Value>() {}
// copy constructor for base
Value(const _Value& value) : _ValueBase<_Value>(value) {}
// copy constructor for base
Value(const _ValueBase<_Value>& value) : _ValueBase<_Value>(value) {}
// copy constructor
Value(const Value& value) : _ValueBase<_Value>(value) {}
// create reference to value
explicit Value(json_t* json) : _ValueBase<_Value>(json) {}
// construct Value from input
static inline Value from(const char* value) { return Value::_take(json_string(value)); }
static inline Value from(const std::string& value) { return from(value.c_str()); }
static inline Value from(bool value) { return Value::_take(value ? json_true() : json_false()); }
static inline Value from(signed int value) { return Value::_take(json_integer(value)); }
static inline Value from(unsigned int value) { return Value::_take(json_integer(value)); }
static inline Value from(signed short value) { return Value::_take(json_integer(value)); }
static inline Value from(unsigned short value) { return Value::_take(json_integer(value)); }
static inline Value from(signed long value) { return Value::_take(json_integer(value)); }
static inline Value from(unsigned long value) { return Value::_take(json_integer(value)); }
static inline Value from(float value) { return Value::_take(json_real(value)); }
static inline Value from(double value) { return Value::_take(json_real(value)); }
// create a new empty object
static inline Value object() { return Value::_take(json_object()); }
// create a new empty array
static inline Value array() { return Value::_take(json_array()); }
// create a new null value
static inline Value null() { return Value::_take(json_null()); }
// load a file as a JSON value // load a file as a JSON value
static Value load_file(const char* path, json_error_t* error = 0) { static Value load_file(const char* path, json_error_t* error = 0) {
return Value::_take(json_load_file(path, error)); return Value::_take(json_load_file(path, error));
@ -58,163 +201,13 @@ public:
// write the value to a file // write the value to a file
int save_file(const char* path, int flags = 0) const { int save_file(const char* path, int flags = 0) const {
return json_dump_file(_value, path, flags); return json_dump_file(as_json(), path, flags);
} }
// write the value to a string (caller must deallocate with free()!) // write the value to a string (caller must deallocate with free()!)
char* save_string(int flags = 0) const { char* save_string(int flags = 0) const {
return json_dumps(_value, flags); return json_dumps(as_json(), flags);
} }
// construct Value from input
static Value from(const char* value) { return Value::_take(json_string(value)); }
static Value from(const std::string& value) { return from(value.c_str()); }
static Value from(bool value) { return Value::_take(value ? json_true() : json_false()); }
static Value from(signed int value) { return Value::_take(json_integer(value)); }
static Value from(unsigned int value) { return Value::_take(json_integer(value)); }
static Value from(signed short value) { return Value::_take(json_integer(value)); }
static Value from(unsigned short value) { return Value::_take(json_integer(value)); }
static Value from(signed long value) { return Value::_take(json_integer(value)); }
static Value from(unsigned long value) { return Value::_take(json_integer(value)); }
static Value from(float value) { return Value::_take(json_real(value)); }
static Value from(double value) { return Value::_take(json_real(value)); }
// create a new empty object
static Value object() { return Value::_take(json_object()); }
// create a new empty array
static Value array() { return Value::_take(json_array()); }
// create a new null value
static Value null() { return Value::_take(json_null()); }
// get the underlying json_t
json_t* as_json() const { return _value; }
// check value type
bool is_undefined() const { return _value == 0; }
bool is_object() const { return json_is_object(_value); }
bool is_array() const { return json_is_array(_value); }
bool is_string() const { return json_is_string(_value); }
bool is_integer() const { return json_is_integer(_value); }
bool is_real() const { return json_is_real(_value); }
bool is_number() const { return json_is_number(_value); }
bool is_true() const { return json_is_true(_value); }
bool is_false() const { return json_is_false(_value); }
bool is_boolean() const { return json_is_boolean(_value); }
bool is_null() const { return json_is_null(_value); }
// get size of array or object
unsigned int size() const {
if (is_object())
return json_object_size(_value);
else
return json_array_size(_value);
}
// get value at array index (const version)
const Value at(unsigned int index) const {
return Value(json_array_get(_value, index));
}
const Value operator[](signed int index) const { return at(index); }
const Value operator[](unsigned int index) const { return at(index); }
const Value operator[](signed short index) const { return at(index); }
const Value operator[](unsigned short index) const { return at(index); }
const Value operator[](signed long index) const { return at(index); }
const Value operator[](unsigned long index) const { return at(index); }
// get value at array index (non-const version)
Value at(unsigned int index) {
return Value(json_array_get(_value, index));
}
Value operator[](signed int index) { return at(index); }
Value operator[](unsigned int index) { return at(index); }
Value operator[](signed short index) { return at(index); }
Value operator[](unsigned short index) { return at(index); }
Value operator[](signed long index) { return at(index); }
Value operator[](unsigned long index) { return at(index); }
// get object property
const Value get(const char* key) const {
return Value(json_object_get(_value, key));
}
const Value get(const std::string& key) const { return get(key.c_str()); }
const Value operator[](const char* key) const { return get(key); }
const Value operator[](const std::string& key) const { return get(key.c_str()); }
// clear all array/object values
void clear() {
if (is_object())
json_object_clear(_value);
else
json_array_clear(_value);
}
// get value cast to specified type
const char* as_cstring() const { return json_string_value(_value); }
std::string as_string() const {
const char* tmp = as_cstring();
return tmp == 0 ? "" : tmp;
}
int as_integer() const { return json_integer_value(_value); }
double as_real() const { return json_real_value(_value); }
double as_number() const { return json_number_value(_value); }
bool as_boolean() const { return is_true(); }
// set an object property (converts value to object is not one already)
Value& set_key(const char* key, const Value& value) {
json_object_set(_value, key, value.as_json());
return *this;
}
Value& set_key(const std::string& key, const Value& value) {
return set_key(key.c_str(), value);
}
// set an array index (converts value to object is not one already)
Value& set_at(unsigned int index, const Value& value) {
if (index == size())
json_array_append(_value, value.as_json());
else
json_array_set(_value, index, value.as_json());
return *this;
}
// delete an object key
Value& del_key(const char* key) {
json_object_del(_value, key);
return *this;
}
Value& del_key(const std::string& key) {
return del_key(key.c_str());
}
// delete an item from an array by index
Value& del_at(unsigned int index) {
json_array_remove(_value, index);
return *this;
}
// insert an item into an array at a given index
Value& insert_at(unsigned int index, const Value& value) {
json_array_insert(_value, index, value.as_json());
return *this;
}
private:
// take ownership of a json_t (does not increase reference count)
static Value _take(json_t* json) {
Value v;
v._value = json;
return v;
}
// internal value pointer
json_t* _value;
}; };
// iterators over a JSON object // iterators over a JSON object
@ -284,4 +277,6 @@ std::istream& operator>>(std::istream& is, jansson::Value& value) {
return is; return is;
} }
#include "janssonxx.tcc"
#endif #endif

133
janssonxx.tcc Normal file
View File

@ -0,0 +1,133 @@
// get size of array or object
template <typename _Base>
unsigned int jansson::_ValueBase<_Base>::size() const {
if (is_object())
return json_object_size(_Base::as_json());
else
return json_array_size(_Base::as_json());
}
// get value at array index (const version)
template <typename _Base>
const jansson::Value jansson::_ValueBase<_Base>::at(unsigned int index) const {
return jansson::Value(json_array_get(_Base::as_json(), index));
}
template <typename _Base>
const jansson::Value jansson::_ValueBase<_Base>::operator[](signed int index) const { return at(index); }
template <typename _Base>
const jansson::Value jansson::_ValueBase<_Base>::operator[](unsigned int index) const { return at(index); }
template <typename _Base>
const jansson::Value jansson::_ValueBase<_Base>::operator[](signed short index) const { return at(index); }
template <typename _Base>
const jansson::Value jansson::_ValueBase<_Base>::operator[](unsigned short index) const { return at(index); }
template <typename _Base>
const jansson::Value jansson::_ValueBase<_Base>::operator[](signed long index) const { return at(index); }
template <typename _Base>
const jansson::Value jansson::_ValueBase<_Base>::operator[](unsigned long index) const { return at(index); }
// get value at array index (non-const version)
template <typename _Base>
jansson::Value jansson::_ValueBase<_Base>::at(unsigned int index) {
return jansson::Value(json_array_get(_Base::as_json(), index));
}
template <typename _Base>
jansson::Value jansson::_ValueBase<_Base>::operator[](signed int index) { return at(index); }
template <typename _Base>
jansson::Value jansson::_ValueBase<_Base>::operator[](unsigned int index) { return at(index); }
template <typename _Base>
jansson::Value jansson::_ValueBase<_Base>::operator[](signed short index) { return at(index); }
template <typename _Base>
jansson::Value jansson::_ValueBase<_Base>::operator[](unsigned short index) { return at(index); }
template <typename _Base>
jansson::Value jansson::_ValueBase<_Base>::operator[](signed long index) { return at(index); }
template <typename _Base>
jansson::Value jansson::_ValueBase<_Base>::operator[](unsigned long index) { return at(index); }
// get object property
template <typename _Base>
const jansson::Value jansson::_ValueBase<_Base>::get(const char* key) const {
return jansson::Value(json_object_get(_Base::as_json(), key));
}
template <typename _Base>
const jansson::Value jansson::_ValueBase<_Base>::get(const std::string& key) const { return get(key.c_str()); }
template <typename _Base>
const jansson::Value jansson::_ValueBase<_Base>::operator[](const char* key) const { return get(key); }
template <typename _Base>
const jansson::Value jansson::_ValueBase<_Base>::operator[](const std::string& key) const { return get(key.c_str()); }
// clear all array/object values
template <typename _Base>
void jansson::_ValueBase<_Base>::clear() {
if (is_object())
json_object_clear(_Base::as_json());
else
json_array_clear(_Base::as_json());
}
// get value cast to specified type
template <typename _Base>
const char* jansson::_ValueBase<_Base>::as_cstring() const { return json_string_value(_Base::as_json()); }
template <typename _Base>
std::string jansson::_ValueBase<_Base>::as_string() const {
const char* tmp = as_cstring();
return tmp == 0 ? "" : tmp;
}
template <typename _Base>
int jansson::_ValueBase<_Base>::as_integer() const { return json_integer_value(_Base::as_json()); }
template <typename _Base>
double jansson::_ValueBase<_Base>::as_real() const { return json_real_value(_Base::as_json()); }
template <typename _Base>
double jansson::_ValueBase<_Base>::as_number() const { return json_number_value(_Base::as_json()); }
template <typename _Base>
bool jansson::_ValueBase<_Base>::as_boolean() const { return is_true(); }
// set an object property (converts value to object is not one already)
template <typename _Base>
_Base& jansson::_ValueBase<_Base>::set_key(const char* key, const jansson::Value& value) {
json_object_set(_Base::as_json(), key, value._Base::as_json());
return *this;
}
template <typename _Base>
_Base& jansson::_ValueBase<_Base>::set_key(const std::string& key, const jansson::Value& value) {
return set_key(key.c_str(), value);
}
// set an array index (converts value to object is not one already)
template <typename _Base>
_Base& jansson::_ValueBase<_Base>::set_at(unsigned int index, const jansson::Value& value) {
if (index == size())
json_array_append(_Base::as_json(), value._Base::as_json());
else
json_array_set(_Base::as_json(), index, value._Base::as_json());
return *this;
}
// delete an object key
template <typename _Base>
_Base& jansson::_ValueBase<_Base>::del_key(const char* key) {
json_object_del(_Base::as_json(), key);
return *this;
}
template <typename _Base>
_Base& jansson::_ValueBase<_Base>::del_key(const std::string& key) {
return del_key(key.c_str());
}
// delete an item from an array by index
template <typename _Base>
_Base& jansson::_ValueBase<_Base>::del_at(unsigned int index) {
json_array_remove(_Base::as_json(), index);
return *this;
}
// insert an item into an array at a given index
template <typename _Base>
_Base& jansson::_ValueBase<_Base>::insert_at(unsigned int index, const jansson::Value& value) {
json_array_insert(_Base::as_json(), index, value._Base::as_json());
return *this;
}

10
test.cc
View File

@ -141,5 +141,15 @@ int main() {
outstr << e12; outstr << e12;
ASSERT_EQ(instr.str(), "{\"bar\": 3,\"foo\": \"test\"}\n", "object did not serialize as expected"); ASSERT_EQ(instr.str(), "{\"bar\": 3,\"foo\": \"test\"}\n", "object did not serialize as expected");
const jansson::Value e13(e12);
ASSERT_EQ(e13["bar"].as_integer(), 3, "e13.bar has incorrect value after copy");
jansson::Value e14(jansson::Value::object());
ASSERT_TRUE(e14.is_object(), "e14 is not an object after construction");
e14.set_key("foo", jansson::Value::object());
ASSERT_TRUE(e14["foo"].is_object(), "e14.foo is not an object after assignment");
//e14["foo"]["bar"] = jansson::Value::from(42);
//ASSERT_EQ(e14["foo"]["bar"].as_integer(), 42, "e14.foo.bar has incorrecy value after assignment");
return 0; return 0;
} }