C++: Untabify, reindent, delete trailing whitespace
This commit is contained in:
parent
5b1a666cf1
commit
2b43e7dbda
@ -4,466 +4,466 @@
|
|||||||
// it under the terms of the MIT license. See LICENSE for details.
|
// it under the terms of the MIT license. See LICENSE for details.
|
||||||
|
|
||||||
#if !defined(IN_JANSSON_HPP)
|
#if !defined(IN_JANSSON_HPP)
|
||||||
# error "jansson-impl.hpp may only by included from jansson.hpp"
|
#error "jansson-impl.hpp may only by included from jansson.hpp"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace json {
|
namespace json {
|
||||||
namespace _private {
|
namespace _private {
|
||||||
// assignment operator
|
// assignment operator
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
ValueBase<_Base>& ValueBase<_Base>::operator=(const Value& value) {
|
ValueBase<_Base>& ValueBase<_Base>::operator=(const Value& value) {
|
||||||
_Base::operator=(value);
|
_Base::operator=(value);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check value type
|
// check value type
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
bool ValueBase<_Base>::is_undefined() const {
|
bool ValueBase<_Base>::is_undefined() const {
|
||||||
return _Base::as_json() == 0;
|
return _Base::as_json() == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
bool ValueBase<_Base>::is_object() const {
|
bool ValueBase<_Base>::is_object() const {
|
||||||
return json_is_object(_Base::as_json());
|
return json_is_object(_Base::as_json());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
bool ValueBase<_Base>::is_array() const {
|
bool ValueBase<_Base>::is_array() const {
|
||||||
return json_is_array(_Base::as_json());
|
return json_is_array(_Base::as_json());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
bool ValueBase<_Base>::is_string() const {
|
bool ValueBase<_Base>::is_string() const {
|
||||||
return json_is_string(_Base::as_json());
|
return json_is_string(_Base::as_json());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
bool ValueBase<_Base>::is_integer() const {
|
bool ValueBase<_Base>::is_integer() const {
|
||||||
return json_is_integer(_Base::as_json());
|
return json_is_integer(_Base::as_json());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
bool ValueBase<_Base>::is_real() const {
|
bool ValueBase<_Base>::is_real() const {
|
||||||
return json_is_real(_Base::as_json());
|
return json_is_real(_Base::as_json());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
bool ValueBase<_Base>::is_number() const {
|
bool ValueBase<_Base>::is_number() const {
|
||||||
return json_is_number(_Base::as_json());
|
return json_is_number(_Base::as_json());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
bool ValueBase<_Base>::is_true() const {
|
bool ValueBase<_Base>::is_true() const {
|
||||||
return json_is_true(_Base::as_json());
|
return json_is_true(_Base::as_json());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
bool ValueBase<_Base>::is_false() const {
|
bool ValueBase<_Base>::is_false() const {
|
||||||
return json_is_false(_Base::as_json());
|
return json_is_false(_Base::as_json());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
bool ValueBase<_Base>::is_boolean() const {
|
bool ValueBase<_Base>::is_boolean() const {
|
||||||
return json_is_boolean(_Base::as_json());
|
return json_is_boolean(_Base::as_json());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
bool ValueBase<_Base>::is_null() const {
|
bool ValueBase<_Base>::is_null() const {
|
||||||
return json_is_null(_Base::as_json());
|
return json_is_null(_Base::as_json());
|
||||||
}
|
}
|
||||||
|
|
||||||
// get size of array or object
|
// get size of array or object
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
unsigned int ValueBase<_Base>::size() const {
|
unsigned int ValueBase<_Base>::size() const {
|
||||||
if (is_object())
|
if (is_object())
|
||||||
return json_object_size(_Base::as_json());
|
return json_object_size(_Base::as_json());
|
||||||
else
|
else
|
||||||
return json_array_size(_Base::as_json());
|
return json_array_size(_Base::as_json());
|
||||||
}
|
}
|
||||||
|
|
||||||
// get value at array index (const version)
|
// get value at array index (const version)
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
const Value ValueBase<_Base>::at(unsigned int index) const {
|
const Value ValueBase<_Base>::at(unsigned int index) const {
|
||||||
return Value(json_array_get(_Base::as_json(), index));
|
return Value(json_array_get(_Base::as_json(), index));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
const Value ValueBase<_Base>::operator[](signed int index) const { return at(index); }
|
const Value ValueBase<_Base>::operator[](signed int index) const { return at(index); }
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
const Value ValueBase<_Base>::operator[](unsigned int index) const { return at(index); }
|
const Value ValueBase<_Base>::operator[](unsigned int index) const { return at(index); }
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
const Value ValueBase<_Base>::operator[](signed short index) const { return at(index); }
|
const Value ValueBase<_Base>::operator[](signed short index) const { return at(index); }
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
const Value ValueBase<_Base>::operator[](unsigned short index) const { return at(index); }
|
const Value ValueBase<_Base>::operator[](unsigned short index) const { return at(index); }
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
const Value ValueBase<_Base>::operator[](signed long index) const { return at(index); }
|
const Value ValueBase<_Base>::operator[](signed long index) const { return at(index); }
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
const Value ValueBase<_Base>::operator[](unsigned long index) const { return at(index); }
|
const Value ValueBase<_Base>::operator[](unsigned long index) const { return at(index); }
|
||||||
|
|
||||||
// get value at array index (non-const version)
|
// get value at array index (non-const version)
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
ValueBase<ElementProxy> ValueBase<_Base>::at(unsigned int index) {
|
ValueBase<ElementProxy> ValueBase<_Base>::at(unsigned int index) {
|
||||||
return ElementProxy(_Base::as_json(), index);
|
return ElementProxy(_Base::as_json(), index);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
ValueBase<ElementProxy> ValueBase<_Base>::operator[](signed int index) {
|
ValueBase<ElementProxy> ValueBase<_Base>::operator[](signed int index) {
|
||||||
return at(index);
|
return at(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
ValueBase<ElementProxy> ValueBase<_Base>::operator[](unsigned int index) {
|
ValueBase<ElementProxy> ValueBase<_Base>::operator[](unsigned int index) {
|
||||||
return at(index);
|
return at(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
ValueBase<ElementProxy> ValueBase<_Base>::operator[](signed short index) {
|
ValueBase<ElementProxy> ValueBase<_Base>::operator[](signed short index) {
|
||||||
return at(index);
|
return at(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
ValueBase<ElementProxy> ValueBase<_Base>::operator[](unsigned short index) {
|
ValueBase<ElementProxy> ValueBase<_Base>::operator[](unsigned short index) {
|
||||||
return at(index);
|
return at(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
ValueBase<ElementProxy> ValueBase<_Base>::operator[](signed long index) {
|
ValueBase<ElementProxy> ValueBase<_Base>::operator[](signed long index) {
|
||||||
return at(index);
|
return at(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
ValueBase<ElementProxy> ValueBase<_Base>::operator[](unsigned long index) {
|
ValueBase<ElementProxy> ValueBase<_Base>::operator[](unsigned long index) {
|
||||||
return at(index);
|
return at(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
// get object property (const version)
|
// get object property (const version)
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
const Value ValueBase<_Base>::get(const char* key) const {
|
const Value ValueBase<_Base>::get(const char* key) const {
|
||||||
return Value(json_object_get(_Base::as_json(), key));
|
return Value(json_object_get(_Base::as_json(), key));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
const Value ValueBase<_Base>::get(const std::string& key) const {
|
const Value ValueBase<_Base>::get(const std::string& key) const {
|
||||||
return get(key.c_str());
|
return get(key.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
const Value ValueBase<_Base>::operator[](const char* key) const {
|
const Value ValueBase<_Base>::operator[](const char* key) const {
|
||||||
return get(key);
|
return get(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
const Value ValueBase<_Base>::operator[](const std::string& key) const {
|
const Value ValueBase<_Base>::operator[](const std::string& key) const {
|
||||||
return get(key.c_str());
|
return get(key.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
// get object property (non-const version)
|
// get object property (non-const version)
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
ValueBase<PropertyProxy> ValueBase<_Base>::get(const char* key) {
|
ValueBase<PropertyProxy> ValueBase<_Base>::get(const char* key) {
|
||||||
return PropertyProxy(_Base::as_json(), key);
|
return PropertyProxy(_Base::as_json(), key);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
ValueBase<PropertyProxy> ValueBase<_Base>::get(const std::string& key) {
|
ValueBase<PropertyProxy> ValueBase<_Base>::get(const std::string& key) {
|
||||||
return get(key.c_str());
|
return get(key.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
ValueBase<PropertyProxy> ValueBase<_Base>::operator[](const char* key) {
|
ValueBase<PropertyProxy> ValueBase<_Base>::operator[](const char* key) {
|
||||||
return get(key);
|
return get(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
ValueBase<PropertyProxy> ValueBase<_Base>::operator[](const std::string& key) {
|
ValueBase<PropertyProxy> ValueBase<_Base>::operator[](const std::string& key) {
|
||||||
return get(key.c_str());
|
return get(key.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
// clear all array/object values
|
// clear all array/object values
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
void ValueBase<_Base>::clear() {
|
void ValueBase<_Base>::clear() {
|
||||||
if (is_object())
|
if (is_object())
|
||||||
json_object_clear(_Base::as_json());
|
json_object_clear(_Base::as_json());
|
||||||
else
|
else
|
||||||
json_array_clear(_Base::as_json());
|
json_array_clear(_Base::as_json());
|
||||||
}
|
}
|
||||||
|
|
||||||
// get value cast to specified type
|
// get value cast to specified type
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
const char* ValueBase<_Base>::as_cstring() const {
|
const char* ValueBase<_Base>::as_cstring() const {
|
||||||
return json_string_value(_Base::as_json());
|
return json_string_value(_Base::as_json());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
std::string ValueBase<_Base>::as_string() const {
|
std::string ValueBase<_Base>::as_string() const {
|
||||||
const char* tmp = as_cstring();
|
const char* tmp = as_cstring();
|
||||||
return tmp == 0 ? "" : tmp;
|
return tmp == 0 ? "" : tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
int ValueBase<_Base>::as_integer() const {
|
int ValueBase<_Base>::as_integer() const {
|
||||||
return json_integer_value(_Base::as_json());
|
return json_integer_value(_Base::as_json());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
double ValueBase<_Base>::as_real() const {
|
double ValueBase<_Base>::as_real() const {
|
||||||
return json_real_value(_Base::as_json());
|
return json_real_value(_Base::as_json());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
double ValueBase<_Base>::as_number() const {
|
double ValueBase<_Base>::as_number() const {
|
||||||
return json_number_value(_Base::as_json());
|
return json_number_value(_Base::as_json());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
bool ValueBase<_Base>::as_boolean() const {
|
bool ValueBase<_Base>::as_boolean() const {
|
||||||
return is_true();
|
return is_true();
|
||||||
}
|
}
|
||||||
|
|
||||||
// set an object property (converts value to object is not one already)
|
// set an object property (converts value to object is not one already)
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
_Base& ValueBase<_Base>::set_key(const char* key, const Value& value) {
|
_Base& ValueBase<_Base>::set_key(const char* key, const Value& value) {
|
||||||
json_object_set(_Base::as_json(), key, value._Base::as_json());
|
json_object_set(_Base::as_json(), key, value._Base::as_json());
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
_Base& ValueBase<_Base>::set_key(const std::string& key, const Value& value) {
|
_Base& ValueBase<_Base>::set_key(const std::string& key, const Value& value) {
|
||||||
return set_key(key.c_str(), value);
|
return set_key(key.c_str(), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
// set an array index (converts value to object is not one already)
|
// set an array index (converts value to object is not one already)
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
_Base& ValueBase<_Base>::set_at(unsigned int index, const Value& value) {
|
_Base& ValueBase<_Base>::set_at(unsigned int index, const Value& value) {
|
||||||
if (index == size())
|
if (index == size())
|
||||||
json_array_append(_Base::as_json(), value._Base::as_json());
|
json_array_append(_Base::as_json(), value._Base::as_json());
|
||||||
else
|
else
|
||||||
json_array_set(_Base::as_json(), index, value._Base::as_json());
|
json_array_set(_Base::as_json(), index, value._Base::as_json());
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete an object key
|
// delete an object key
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
_Base& ValueBase<_Base>::del_key(const char* key) {
|
_Base& ValueBase<_Base>::del_key(const char* key) {
|
||||||
json_object_del(_Base::as_json(), key);
|
json_object_del(_Base::as_json(), key);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
_Base& ValueBase<_Base>::del_key(const std::string& key) {
|
_Base& ValueBase<_Base>::del_key(const std::string& key) {
|
||||||
return del_key(key.c_str());
|
return del_key(key.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete an item from an array by index
|
// delete an item from an array by index
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
_Base& ValueBase<_Base>::del_at(unsigned int index) {
|
_Base& ValueBase<_Base>::del_at(unsigned int index) {
|
||||||
json_array_remove(_Base::as_json(), index);
|
json_array_remove(_Base::as_json(), index);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// insert an item into an array at a given index
|
// insert an item into an array at a given index
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
_Base& ValueBase<_Base>::insert_at(unsigned int index, const Value& value) {
|
_Base& ValueBase<_Base>::insert_at(unsigned int index, const Value& value) {
|
||||||
json_array_insert(_Base::as_json(), index, value._Base::as_json());
|
json_array_insert(_Base::as_json(), index, value._Base::as_json());
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// write the value to a file
|
// write the value to a file
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
int ValueBase<_Base>::save_file(const char* path, int flags) const {
|
int ValueBase<_Base>::save_file(const char* path, int flags) const {
|
||||||
return json_dump_file(_Base::as_json(), path, flags);
|
return json_dump_file(_Base::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()!)
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
char* ValueBase<_Base>::save_string(int flags) const {
|
char* ValueBase<_Base>::save_string(int flags) const {
|
||||||
return json_dumps(_Base::as_json(), flags);
|
return json_dumps(_Base::as_json(), flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
Basic::~Basic() {
|
Basic::~Basic() {
|
||||||
json_decref(_value);
|
json_decref(_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
// copy an existing Value
|
// copy an existing Value
|
||||||
Basic& Basic::operator=(const Basic& e) {
|
Basic& Basic::operator=(const Basic& e) {
|
||||||
if (&e != this) {
|
if (&e != this) {
|
||||||
json_decref(_value);
|
json_decref(_value);
|
||||||
_value = json_incref(e._value);
|
_value = json_incref(e._value);
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the underlying json_t
|
// get the underlying json_t
|
||||||
json_t* Basic::as_json() const {
|
json_t* Basic::as_json() const {
|
||||||
return _value;
|
return _value;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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_ownership(json_t* json) {
|
Basic Basic::take_ownership(json_t* json) {
|
||||||
Basic v;
|
Basic v;
|
||||||
v._value = json;
|
v._value = json;
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
// assign value to proxied array element
|
// assign value to proxied array element
|
||||||
ElementProxy& ElementProxy::operator=(const Value& value) {
|
ElementProxy& ElementProxy::operator=(const Value& value) {
|
||||||
json_array_set(_array, _index, value.as_json());
|
json_array_set(_array, _index, value.as_json());
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the proxied element
|
// get the proxied element
|
||||||
json_t* ElementProxy::as_json() const {
|
json_t* ElementProxy::as_json() const {
|
||||||
return json_array_get(_array, _index);
|
return json_array_get(_array, _index);
|
||||||
}
|
}
|
||||||
|
|
||||||
// assign value to proxied object property
|
// assign value to proxied object property
|
||||||
PropertyProxy& PropertyProxy::operator=(const Value& value) {
|
PropertyProxy& PropertyProxy::operator=(const Value& value) {
|
||||||
json_object_set(_object, _key, value.as_json());
|
json_object_set(_object, _key, value.as_json());
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
json_t* PropertyProxy::as_json() const {
|
json_t* PropertyProxy::as_json() const {
|
||||||
return json_object_get(_object, _key);
|
return json_object_get(_object, _key);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace json::_private
|
} // namespace json::_private
|
||||||
|
|
||||||
// construct Value::Value input
|
// construct Value::Value input
|
||||||
Value::Value(const char* value) {
|
Value::Value(const char* value) {
|
||||||
_value = json_string(value);
|
_value = json_string(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value::Value(const std::string& value) {
|
Value::Value(const std::string& value) {
|
||||||
_value = json_string(value.c_str());
|
_value = json_string(value.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
Value::Value(bool value) {
|
Value::Value(bool value) {
|
||||||
_value = value ? json_true() : json_false();
|
_value = value ? json_true() : json_false();
|
||||||
}
|
}
|
||||||
|
|
||||||
Value::Value(signed int value) {
|
Value::Value(signed int value) {
|
||||||
_value = json_integer(value);
|
_value = json_integer(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value::Value(unsigned int value) {
|
Value::Value(unsigned int value) {
|
||||||
_value = json_integer(value);
|
_value = json_integer(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value::Value(signed short value) {
|
Value::Value(signed short value) {
|
||||||
_value = json_integer(value);
|
_value = json_integer(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value::Value(unsigned short value) {
|
Value::Value(unsigned short value) {
|
||||||
_value = json_integer(value);
|
_value = json_integer(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value::Value(signed long value) {
|
Value::Value(signed long value) {
|
||||||
_value = json_integer(value);
|
_value = json_integer(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value::Value(unsigned long value) {
|
Value::Value(unsigned long value) {
|
||||||
_value = json_integer(value);
|
_value = json_integer(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value::Value(float value) {
|
Value::Value(float value) {
|
||||||
_value = json_real(value);
|
_value = json_real(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value::Value(double value) {
|
Value::Value(double value) {
|
||||||
_value = json_real(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());
|
||||||
}
|
}
|
||||||
|
|
||||||
// construct a new iterator for a given object
|
// construct a new iterator for a given object
|
||||||
Iterator::Iterator(const _private::ValueBase<_private::PropertyProxy>& value) :
|
Iterator::Iterator(const _private::ValueBase<_private::PropertyProxy>& value) :
|
||||||
_object(value.as_json()), _iter(0) {
|
_object(value.as_json()), _iter(0) {
|
||||||
_iter = json_object_iter(_object.as_json());
|
_iter = json_object_iter(_object.as_json());
|
||||||
}
|
}
|
||||||
|
|
||||||
// increment iterator
|
// increment iterator
|
||||||
void Iterator::next() {
|
void Iterator::next() {
|
||||||
_iter = json_object_iter_next(_object.as_json(), _iter);
|
_iter = json_object_iter_next(_object.as_json(), _iter);
|
||||||
}
|
}
|
||||||
|
|
||||||
Iterator& Iterator::operator++() { next(); return *this; }
|
Iterator& Iterator::operator++() { next(); return *this; }
|
||||||
|
|
||||||
// test if iterator is still valid
|
// test if iterator is still valid
|
||||||
bool Iterator::valid() const {
|
bool Iterator::valid() const {
|
||||||
return _iter != 0;
|
return _iter != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Iterator::operator bool() const {
|
Iterator::operator bool() const {
|
||||||
return valid();
|
return valid();
|
||||||
}
|
}
|
||||||
|
|
||||||
// get key
|
// get key
|
||||||
const char* Iterator::ckey() const {
|
const char* Iterator::ckey() const {
|
||||||
return json_object_iter_key(_iter);
|
return json_object_iter_key(_iter);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Iterator::key() const {
|
std::string Iterator::key() const {
|
||||||
return ckey();
|
return ckey();
|
||||||
}
|
}
|
||||||
|
|
||||||
// get value
|
// get value
|
||||||
const Value Iterator::value() const {
|
const Value Iterator::value() const {
|
||||||
return Value(json_object_iter_value(_iter));
|
return Value(json_object_iter_value(_iter));
|
||||||
}
|
}
|
||||||
|
|
||||||
// dereference value
|
// dereference value
|
||||||
const Value Iterator::operator*() const {
|
const Value Iterator::operator*() const {
|
||||||
return value();
|
return 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());
|
||||||
}
|
}
|
||||||
|
|
||||||
// create a new empty array
|
// create a new empty array
|
||||||
Value array() {
|
Value array() {
|
||||||
return Value::take_ownership(json_array());
|
return Value::take_ownership(json_array());
|
||||||
}
|
}
|
||||||
|
|
||||||
// create a new null value
|
// create a new null value
|
||||||
Value null() {
|
Value null() {
|
||||||
return Value::take_ownership(json_null());
|
return Value::take_ownership(json_null());
|
||||||
}
|
}
|
||||||
|
|
||||||
// load a file as a JSON value
|
// load a file as a JSON value
|
||||||
Value load_file(const char* path, json_error_t* error) {
|
Value load_file(const char* path, json_error_t* error) {
|
||||||
return Value::take_ownership(json_load_file(path, error));
|
return Value::take_ownership(json_load_file(path, error));
|
||||||
}
|
}
|
||||||
|
|
||||||
// load a string as a JSON value
|
// load a string as a JSON value
|
||||||
Value load_string(const char* string, json_error_t* error) {
|
Value load_string(const char* string, json_error_t* error) {
|
||||||
return Value::take_ownership(json_loads(string, error));
|
return Value::take_ownership(json_loads(string, error));
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace json
|
} // namespace json
|
||||||
|
|
||||||
// stream JSON value out
|
// stream JSON value out
|
||||||
std::ostream& operator<<(std::ostream& os, const json::Value& value) {
|
std::ostream& operator<<(std::ostream& os, const json::Value& value) {
|
||||||
// get the temporary serialize string
|
// get the temporary serialize string
|
||||||
char* tmp = value.save_string();
|
char* tmp = value.save_string();
|
||||||
if (tmp != 0) {
|
if (tmp != 0) {
|
||||||
// stream temp string out and release it
|
// stream temp string out and release it
|
||||||
os << tmp;
|
os << tmp;
|
||||||
free(tmp);
|
free(tmp);
|
||||||
}
|
}
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
// read JSON value
|
// read JSON value
|
||||||
std::istream& operator>>(std::istream& is, json::Value& value) {
|
std::istream& operator>>(std::istream& is, json::Value& value) {
|
||||||
// buffer the remaining bytes into a single string for Jansson
|
// buffer the remaining bytes into a single string for Jansson
|
||||||
std::stringstream tmp;
|
std::stringstream tmp;
|
||||||
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::load_string(tmp.str().c_str());
|
value = json::load_string(tmp.str().c_str());
|
||||||
return is;
|
return is;
|
||||||
}
|
}
|
||||||
|
440
src/jansson.hpp
440
src/jansson.hpp
@ -13,279 +13,279 @@
|
|||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
|
||||||
namespace json {
|
namespace json {
|
||||||
// include Jansson C library into the json namespace
|
// include Jansson C library into the json namespace
|
||||||
#include <jansson.h>
|
# include <jansson.h>
|
||||||
|
|
||||||
class Iterator;
|
class Iterator;
|
||||||
class Value;
|
class Value;
|
||||||
|
|
||||||
// implementation details; do not use directly
|
// implementation details; do not use directly
|
||||||
namespace _private {
|
namespace _private {
|
||||||
class ElementProxy;
|
class ElementProxy;
|
||||||
class PropertyProxy;
|
class PropertyProxy;
|
||||||
|
|
||||||
// base class for JSON value interface
|
// base class for JSON value interface
|
||||||
template <typename _Base>
|
template <typename _Base>
|
||||||
class ValueBase : public _Base {
|
class ValueBase : public _Base {
|
||||||
public:
|
public:
|
||||||
// empty constructor
|
// empty constructor
|
||||||
ValueBase() : _Base() {}
|
ValueBase() : _Base() {}
|
||||||
|
|
||||||
// copy constructor
|
// copy constructor
|
||||||
ValueBase(const _Base& base) : _Base(base) {}
|
ValueBase(const _Base& base) : _Base(base) {}
|
||||||
|
|
||||||
// create reference to value
|
// create reference to value
|
||||||
ValueBase(json_t* json) : _Base(json) {}
|
ValueBase(json_t* json) : _Base(json) {}
|
||||||
|
|
||||||
// assignment operator
|
// assignment operator
|
||||||
inline ValueBase& operator=(const Value& value);
|
inline ValueBase& operator=(const Value& value);
|
||||||
|
|
||||||
// check value type
|
// check value type
|
||||||
inline bool is_undefined() const;
|
inline bool is_undefined() const;
|
||||||
inline bool is_object() const;
|
inline bool is_object() const;
|
||||||
inline bool is_array() const;
|
inline bool is_array() const;
|
||||||
inline bool is_string() const;
|
inline bool is_string() const;
|
||||||
inline bool is_integer() const;
|
inline bool is_integer() const;
|
||||||
inline bool is_real() const;
|
inline bool is_real() const;
|
||||||
inline bool is_number() const;
|
inline bool is_number() const;
|
||||||
inline bool is_true() const;
|
inline bool is_true() const;
|
||||||
inline bool is_false() const;
|
inline bool is_false() const;
|
||||||
inline bool is_boolean() const;
|
inline bool is_boolean() const;
|
||||||
inline bool is_null() const;
|
inline bool is_null() const;
|
||||||
|
|
||||||
// get size of array or object
|
// get size of array or object
|
||||||
inline unsigned int size() const;
|
inline unsigned int size() const;
|
||||||
|
|
||||||
// get value at array index (const version)
|
// get value at array index (const version)
|
||||||
inline const Value at(unsigned int index) const;
|
inline const Value at(unsigned int index) const;
|
||||||
|
|
||||||
inline const Value operator[](signed int index) const;
|
inline const Value operator[](signed int index) const;
|
||||||
inline const Value operator[](unsigned int index) const;
|
inline const Value operator[](unsigned int index) const;
|
||||||
inline const Value operator[](signed short index) const;
|
inline const Value operator[](signed short index) const;
|
||||||
inline const Value operator[](unsigned short index) const;
|
inline const Value operator[](unsigned short index) const;
|
||||||
inline const Value operator[](signed long index) const;
|
inline const Value operator[](signed long index) const;
|
||||||
inline const Value operator[](unsigned long index) const;
|
inline const Value operator[](unsigned long index) const;
|
||||||
|
|
||||||
// get value at array index (non-const version)
|
// get value at array index (non-const version)
|
||||||
inline ValueBase<ElementProxy> at(unsigned int index);
|
inline ValueBase<ElementProxy> at(unsigned int index);
|
||||||
|
|
||||||
inline ValueBase<ElementProxy> operator[](signed int index);
|
inline ValueBase<ElementProxy> operator[](signed int index);
|
||||||
inline ValueBase<ElementProxy> operator[](unsigned int index);
|
inline ValueBase<ElementProxy> operator[](unsigned int index);
|
||||||
inline ValueBase<ElementProxy> operator[](signed short index);
|
inline ValueBase<ElementProxy> operator[](signed short index);
|
||||||
inline ValueBase<ElementProxy> operator[](unsigned short index);
|
inline ValueBase<ElementProxy> operator[](unsigned short index);
|
||||||
inline ValueBase<ElementProxy> operator[](signed long index);
|
inline ValueBase<ElementProxy> operator[](signed long index);
|
||||||
inline ValueBase<ElementProxy> operator[](unsigned long index);
|
inline ValueBase<ElementProxy> operator[](unsigned long index);
|
||||||
|
|
||||||
// get object property (const version)
|
// get object property (const version)
|
||||||
inline const Value get(const char* key) const;
|
inline const Value get(const char* key) const;
|
||||||
|
|
||||||
inline const Value get(const std::string& key) const;
|
inline const Value get(const std::string& key) const;
|
||||||
inline const Value operator[](const char* key) const;
|
inline const Value operator[](const char* key) const;
|
||||||
inline const Value operator[](const std::string& key) const;
|
inline const Value operator[](const std::string& key) const;
|
||||||
|
|
||||||
// get object property (non-const version)
|
// get object property (non-const version)
|
||||||
inline ValueBase<PropertyProxy> get(const char* key);
|
inline ValueBase<PropertyProxy> get(const char* key);
|
||||||
|
|
||||||
inline ValueBase<PropertyProxy> get(const std::string& key);
|
inline ValueBase<PropertyProxy> get(const std::string& key);
|
||||||
inline ValueBase<PropertyProxy> operator[](const char* key);
|
inline ValueBase<PropertyProxy> operator[](const char* key);
|
||||||
inline ValueBase<PropertyProxy> operator[](const std::string& key);
|
inline ValueBase<PropertyProxy> operator[](const std::string& key);
|
||||||
|
|
||||||
// clear all array/object values
|
// clear all array/object values
|
||||||
inline void clear();
|
inline void clear();
|
||||||
|
|
||||||
// get value cast to specified type
|
// get value cast to specified type
|
||||||
inline const char* as_cstring() const;
|
inline const char* as_cstring() const;
|
||||||
inline std::string as_string() const;
|
inline std::string as_string() const;
|
||||||
inline int as_integer() const;
|
inline int as_integer() const;
|
||||||
inline double as_real() const;
|
inline double as_real() const;
|
||||||
inline double as_number() const;
|
inline double as_number() const;
|
||||||
inline bool as_boolean() const;
|
inline bool as_boolean() const;
|
||||||
|
|
||||||
// set an object property (converts value to object is not one already)
|
// 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 char* key, const Value& value);
|
||||||
|
|
||||||
inline _Base& set_key(const std::string& 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)
|
// set an array index (converts value to object is not one already)
|
||||||
inline _Base& set_at(unsigned int index, const Value& value);
|
inline _Base& set_at(unsigned int index, const Value& value);
|
||||||
|
|
||||||
// delete an object key
|
// delete an object key
|
||||||
inline _Base& del_key(const char* key);
|
inline _Base& del_key(const char* key);
|
||||||
|
|
||||||
inline _Base& del_key(const std::string& key);
|
inline _Base& del_key(const std::string& key);
|
||||||
|
|
||||||
// delete an item from an array by index
|
// delete an item from an array by index
|
||||||
inline _Base& del_at(unsigned int index);
|
inline _Base& del_at(unsigned int index);
|
||||||
|
|
||||||
// insert an item into an array at a given index
|
// insert an item into an array at a given index
|
||||||
inline _Base& insert_at(unsigned int index, const Value& value);
|
inline _Base& insert_at(unsigned int index, const Value& value);
|
||||||
|
|
||||||
// write the value to a file
|
// write the value to a file
|
||||||
inline int save_file(const char* path, int flags = 0) const;
|
inline int save_file(const char* path, int flags = 0) const;
|
||||||
|
|
||||||
// write the value to a string (caller must deallocate with free()!)
|
// write the value to a string (caller must deallocate with free()!)
|
||||||
inline char* save_string(int flags = 0) const;
|
inline char* save_string(int flags = 0) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
// represents any JSON value, private base
|
// represents any JSON value, private base
|
||||||
class Basic {
|
class Basic {
|
||||||
public:
|
public:
|
||||||
// construct new Value with an undefined value
|
// construct new Value with an undefined value
|
||||||
Basic() : _value(0) {}
|
Basic() : _value(0) {}
|
||||||
|
|
||||||
// copy constructor
|
// copy constructor
|
||||||
Basic(const Basic& value) : _value(json_incref(value._value)) {}
|
Basic(const Basic& value) : _value(json_incref(value._value)) {}
|
||||||
|
|
||||||
// make a reference to an existing json_t value
|
// make a reference to an existing json_t value
|
||||||
explicit Basic(json_t* value) : _value(json_incref(value)) {}
|
explicit Basic(json_t* value) : _value(json_incref(value)) {}
|
||||||
|
|
||||||
// free Value resources
|
// free Value resources
|
||||||
inline ~Basic();
|
inline ~Basic();
|
||||||
|
|
||||||
// copy an existing Value
|
// copy an existing Value
|
||||||
inline Basic& operator=(const Basic& e);
|
inline Basic& operator=(const Basic& e);
|
||||||
|
|
||||||
// get the underlying json_t
|
// get the underlying json_t
|
||||||
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)
|
||||||
inline static Basic take_ownership(json_t* json);
|
inline static Basic take_ownership(json_t* json);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// internal value pointer
|
// internal value pointer
|
||||||
json_t* _value;
|
json_t* _value;
|
||||||
};
|
};
|
||||||
|
|
||||||
// proxies an array element
|
// proxies an array element
|
||||||
class ElementProxy {
|
class ElementProxy {
|
||||||
public:
|
public:
|
||||||
// constructor
|
// constructor
|
||||||
ElementProxy(json_t* array, unsigned int index) : _array(array), _index(index) {}
|
ElementProxy(json_t* array, unsigned int index) : _array(array), _index(index) {}
|
||||||
|
|
||||||
// assign to the proxied element
|
// assign to the proxied element
|
||||||
inline ElementProxy& operator=(const Value& value);
|
inline ElementProxy& operator=(const Value& value);
|
||||||
|
|
||||||
// get the proxied element
|
// get the proxied element
|
||||||
inline json_t* as_json() const;
|
inline json_t* as_json() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// array object we wrap
|
// array object we wrap
|
||||||
json_t* _array;
|
json_t* _array;
|
||||||
|
|
||||||
// index of property
|
// index of property
|
||||||
unsigned int _index;
|
unsigned int _index;
|
||||||
};
|
};
|
||||||
|
|
||||||
// proxies an object property
|
// proxies an object property
|
||||||
class PropertyProxy {
|
class PropertyProxy {
|
||||||
public:
|
public:
|
||||||
// constructor
|
// constructor
|
||||||
PropertyProxy(json_t* array, const char* key) : _object(array), _key(key) {}
|
PropertyProxy(json_t* array, const char* key) : _object(array), _key(key) {}
|
||||||
|
|
||||||
// assign to the proxied element
|
// assign to the proxied element
|
||||||
inline PropertyProxy& operator=(const Value& value);
|
inline PropertyProxy& operator=(const Value& value);
|
||||||
|
|
||||||
// get the proxied element
|
// get the proxied element
|
||||||
inline json_t* as_json() const;
|
inline json_t* as_json() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// array object we wrap
|
// array object we wrap
|
||||||
json_t* _object;
|
json_t* _object;
|
||||||
|
|
||||||
// key of property
|
// key of property
|
||||||
const char* _key;
|
const char* _key;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace json::_private
|
} // namespace json::_private
|
||||||
|
|
||||||
// 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
|
// construct Value from input
|
||||||
explicit inline Value(const char* value);
|
explicit inline Value(const char* value);
|
||||||
explicit inline Value(const std::string& value);
|
explicit inline Value(const std::string& value);
|
||||||
explicit inline Value(bool value);
|
explicit inline Value(bool value);
|
||||||
explicit inline Value(signed int value);
|
explicit inline Value(signed int value);
|
||||||
explicit inline Value(unsigned int value);
|
explicit inline Value(unsigned int value);
|
||||||
explicit inline Value(signed short value);
|
explicit inline Value(signed short value);
|
||||||
explicit inline Value(unsigned short value);
|
explicit inline Value(unsigned short value);
|
||||||
explicit inline Value(signed long value);
|
explicit inline Value(signed long value);
|
||||||
explicit inline Value(unsigned long value);
|
explicit inline Value(unsigned long value);
|
||||||
explicit inline Value(float value);
|
explicit inline Value(float value);
|
||||||
explicit inline Value(double value);
|
explicit inline Value(double value);
|
||||||
|
|
||||||
// empty constructor
|
// empty constructor
|
||||||
Value() : _private::ValueBase<_private::Basic>() {}
|
Value() : _private::ValueBase<_private::Basic>() {}
|
||||||
|
|
||||||
// copy constructor for base
|
// copy constructor for base
|
||||||
Value(const _private::Basic& value) : _private::ValueBase<_private::Basic>(value) {}
|
Value(const _private::Basic& value) : _private::ValueBase<_private::Basic>(value) {}
|
||||||
|
|
||||||
// copy constructor for base
|
// copy constructor for base
|
||||||
Value(const _private::ValueBase<_private::Basic>& value) : _private::ValueBase<_private::Basic>(value) {}
|
Value(const _private::ValueBase<_private::Basic>& value) : _private::ValueBase<_private::Basic>(value) {}
|
||||||
|
|
||||||
// copy constructor
|
// copy constructor
|
||||||
Value(const Value& value) : _private::ValueBase<_private::Basic>(value) {}
|
Value(const Value& value) : _private::ValueBase<_private::Basic>(value) {}
|
||||||
|
|
||||||
// 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) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
// iterators over a JSON object
|
// iterators over a JSON object
|
||||||
class Iterator {
|
class Iterator {
|
||||||
public:
|
public:
|
||||||
// construct a new iterator for a given object
|
// construct a new iterator for a given object
|
||||||
inline Iterator(const Value& value);
|
inline Iterator(const Value& value);
|
||||||
|
|
||||||
// construct a new iterator for a given object
|
// construct a new iterator for a given object
|
||||||
inline Iterator(const _private::ValueBase<_private::PropertyProxy>& value);
|
inline Iterator(const _private::ValueBase<_private::PropertyProxy>& value);
|
||||||
|
|
||||||
// increment iterator
|
// increment iterator
|
||||||
inline void next();
|
inline void next();
|
||||||
|
|
||||||
inline Iterator& operator++();
|
inline Iterator& operator++();
|
||||||
|
|
||||||
// test if iterator is still valid
|
// test if iterator is still valid
|
||||||
inline bool valid() const;
|
inline bool valid() const;
|
||||||
|
|
||||||
inline operator bool() const;
|
inline operator bool() const;
|
||||||
|
|
||||||
// get key
|
// get key
|
||||||
inline const char* ckey() const;
|
inline const char* ckey() const;
|
||||||
|
|
||||||
inline std::string key() const;
|
inline std::string key() const;
|
||||||
|
|
||||||
// get value
|
// get value
|
||||||
inline const Value value() const;
|
inline const Value value() const;
|
||||||
|
|
||||||
// dereference value
|
// dereference value
|
||||||
inline const Value operator*() const;
|
inline const Value operator*() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// disallow copying
|
// disallow copying
|
||||||
Iterator(const Iterator&);
|
Iterator(const Iterator&);
|
||||||
Iterator& operator=(const Iterator&);
|
Iterator& operator=(const Iterator&);
|
||||||
|
|
||||||
// object being iterated over
|
// object being iterated over
|
||||||
Value _object;
|
Value _object;
|
||||||
|
|
||||||
// iterator value
|
// iterator value
|
||||||
void* _iter;
|
void* _iter;
|
||||||
};
|
};
|
||||||
|
|
||||||
// create a new empty object
|
// create a new empty object
|
||||||
inline Value object();
|
inline Value object();
|
||||||
|
|
||||||
// create a new empty array
|
// create a new empty array
|
||||||
inline Value array();
|
inline Value array();
|
||||||
|
|
||||||
// create a new null value
|
// create a new null value
|
||||||
inline Value null();
|
inline Value null();
|
||||||
|
|
||||||
// load a file as a JSON value
|
// load a file as a JSON value
|
||||||
inline Value load_file(const char* path, json_error_t* error = 0);
|
inline Value load_file(const char* path, json_error_t* error = 0);
|
||||||
|
|
||||||
// load a string as a JSON value
|
// load a string as a JSON value
|
||||||
inline Value load_string(const char* string, json_error_t* error = 0);
|
inline Value load_string(const char* string, json_error_t* error = 0);
|
||||||
|
|
||||||
} // namespace json
|
} // namespace json
|
||||||
|
|
||||||
|
@ -4,156 +4,159 @@
|
|||||||
|
|
||||||
#include "jansson.hpp"
|
#include "jansson.hpp"
|
||||||
|
|
||||||
#define ASSERT_OP(lhs, rhs, op, m) \
|
#define ASSERT_OP(lhs, rhs, op, m) \
|
||||||
do { \
|
do { \
|
||||||
if(!((lhs) op (rhs))) { \
|
if(!((lhs) op (rhs))) { \
|
||||||
std::cerr << std::boolalpha; \
|
std::cerr << std::boolalpha; \
|
||||||
std::cerr << __FILE__ << '[' << __LINE__ << "]: ERROR: " << (m) << std::endl; \
|
std::cerr << __FILE__ << '[' << __LINE__ << "]: ERROR: " \
|
||||||
std::cerr << "\ttest: " << #lhs << ' ' << #op << ' ' << #rhs << std::endl; \
|
<< (m) << std::endl; \
|
||||||
std::cerr << "\tresult: " << (lhs) << ' ' << #op << ' ' << (rhs) << std::endl; \
|
std::cerr << "\ttest: " << #lhs << ' ' << #op << ' ' \
|
||||||
return 1; \
|
<< #rhs << std::endl; \
|
||||||
} \
|
std::cerr << "\tresult: " << (lhs) << ' ' << #op << ' ' \
|
||||||
} while(0)
|
<< (rhs) << std::endl; \
|
||||||
|
return 1; \
|
||||||
|
} \
|
||||||
|
} while(0)
|
||||||
#define ASSERT_EQ(lhs, rhs, m) ASSERT_OP(lhs, rhs, ==, m)
|
#define ASSERT_EQ(lhs, rhs, m) ASSERT_OP(lhs, rhs, ==, m)
|
||||||
#define ASSERT_NE(lhs, rhs, m) ASSERT_OP(lhs, rhs, !=, m)
|
#define ASSERT_NE(lhs, rhs, m) ASSERT_OP(lhs, rhs, !=, m)
|
||||||
#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 main() {
|
||||||
json::Value e1(json::load_file("suites/api/test.json"));
|
json::Value e1(json::load_file("suites/api/test.json"));
|
||||||
json::Value e2(e1);
|
json::Value e2(e1);
|
||||||
json::Value e3;
|
json::Value e3;
|
||||||
json::Value e4(json::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");
|
||||||
ASSERT_TRUE(e3.is_undefined(), "e3 has a defined value");
|
ASSERT_TRUE(e3.is_undefined(), "e3 has a defined value");
|
||||||
ASSERT_TRUE(e4.is_object(), "e4 is not an object");
|
ASSERT_TRUE(e4.is_object(), "e4 is not an object");
|
||||||
|
|
||||||
ASSERT_EQ(e1.size(), 1, "e1 has too many properties");
|
ASSERT_EQ(e1.size(), 1, "e1 has too many properties");
|
||||||
ASSERT_EQ(e2.size(), 1, "e2 has too many properties");
|
ASSERT_EQ(e2.size(), 1, "e2 has too many properties");
|
||||||
ASSERT_EQ(e4.size(), 2, "e4 does not have 2 elements");
|
ASSERT_EQ(e4.size(), 2, "e4 does not have 2 elements");
|
||||||
|
|
||||||
ASSERT_TRUE(e1.get("web-app").is_object(), "e1[0].web-app is not an object");
|
ASSERT_TRUE(e1.get("web-app").is_object(), "e1[0].web-app is not an object");
|
||||||
ASSERT_EQ(e1.get("web-app").get("servlet").at(0).get("servlet-class").as_string(), "org.cofax.cds.CDSServlet", "property has incorrect value");
|
ASSERT_EQ(e1.get("web-app").get("servlet").at(0).get("servlet-class").as_string(), "org.cofax.cds.CDSServlet", "property has incorrect value");
|
||||||
ASSERT_EQ(e1["web-app"]["servlet"][0]["servlet-class"].as_string(), "org.cofax.cds.CDSServlet", "property has incorrect value");
|
ASSERT_EQ(e1["web-app"]["servlet"][0]["servlet-class"].as_string(), "org.cofax.cds.CDSServlet", "property has incorrect value");
|
||||||
|
|
||||||
ASSERT_EQ(e4["foo"].as_boolean(), true, "property has incorrect value");
|
ASSERT_EQ(e4["foo"].as_boolean(), true, "property has incorrect value");
|
||||||
|
|
||||||
json::Iterator i(e1.get("web-app"));
|
json::Iterator i(e1.get("web-app"));
|
||||||
ASSERT_EQ(i.key(), "taglib", "first iterator result has incorrect key");
|
ASSERT_EQ(i.key(), "taglib", "first iterator result has incorrect key");
|
||||||
i.next();
|
i.next();
|
||||||
ASSERT_EQ(i.key(), "servlet", "first iterator result has incorrect key");
|
ASSERT_EQ(i.key(), "servlet", "first iterator result has incorrect key");
|
||||||
i.next();
|
i.next();
|
||||||
ASSERT_EQ(i.key(), "servlet-mapping", "first iterator result has incorrect key");
|
ASSERT_EQ(i.key(), "servlet-mapping", "first iterator result has incorrect key");
|
||||||
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(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::Value(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::Value("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");
|
||||||
|
|
||||||
json::Value e8(json::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::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::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("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::Value("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::Value("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");
|
||||||
|
|
||||||
e10.set_at(100, json::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("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");
|
||||||
|
|
||||||
e10.del_at(0);
|
e10.del_at(0);
|
||||||
ASSERT_EQ(e10.size(), 2, "e10 has incorrect size after delete");
|
ASSERT_EQ(e10.size(), 2, "e10 has incorrect size after delete");
|
||||||
ASSERT_EQ(e10[1].as_string(), "foobar", "e10[1] has incorrect value after delete");
|
ASSERT_EQ(e10[1].as_string(), "foobar", "e10[1] has incorrect value after delete");
|
||||||
|
|
||||||
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::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("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::Value("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::Value("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");
|
||||||
|
|
||||||
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::object());
|
json::Value e12(json::object());
|
||||||
e12.set_key("foo", json::Value("test"));
|
e12.set_key("foo", json::Value("test"));
|
||||||
e12.set_key("bar", json::Value(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);
|
||||||
ASSERT_EQ(out, "{\"bar\": 3, \"foo\": \"test\"}", "object did not serialize as expected");
|
ASSERT_EQ(out, "{\"bar\": 3, \"foo\": \"test\"}", "object did not serialize as expected");
|
||||||
|
|
||||||
std::istringstream instr(out);
|
std::istringstream instr(out);
|
||||||
instr >> e12;
|
instr >> e12;
|
||||||
ASSERT_TRUE(e12.is_object(), "e12 is not an object after stream read");
|
ASSERT_TRUE(e12.is_object(), "e12 is not an object after stream read");
|
||||||
ASSERT_EQ(e12.size(), 2, "e12 has wrong size after stream read");
|
ASSERT_EQ(e12.size(), 2, "e12 has wrong size after stream read");
|
||||||
ASSERT_EQ(e12.get("bar").as_integer(), 3, "e12.bar has incorrect value after stream read");
|
ASSERT_EQ(e12.get("bar").as_integer(), 3, "e12.bar has incorrect value after stream read");
|
||||||
ASSERT_EQ(e12.get("foo").as_string(), "test", "ee12.test has incorrect value after stream read");
|
ASSERT_EQ(e12.get("foo").as_string(), "test", "ee12.test has incorrect value after stream read");
|
||||||
|
|
||||||
std::ostringstream outstr;
|
std::ostringstream outstr;
|
||||||
outstr << e12;
|
outstr << e12;
|
||||||
ASSERT_EQ(instr.str(), "{\"bar\": 3, \"foo\": \"test\"}", "object did not serialize as expected");
|
ASSERT_EQ(instr.str(), "{\"bar\": 3, \"foo\": \"test\"}", "object did not serialize as expected");
|
||||||
|
|
||||||
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::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::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(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::Value(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::Value("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;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user