aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/jansson-impl.hpp900
-rw-r--r--src/jansson.hpp446
2 files changed, 673 insertions, 673 deletions
diff --git a/src/jansson-impl.hpp b/src/jansson-impl.hpp
index dbf94df..de336dd 100644
--- a/src/jansson-impl.hpp
+++ b/src/jansson-impl.hpp
@@ -4,466 +4,466 @@
// it under the terms of the MIT license. See LICENSE for details.
#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
namespace json {
- namespace _private {
- // assignment operator
- template <typename _Base>
- ValueBase<_Base>& ValueBase<_Base>::operator=(const Value& value) {
- _Base::operator=(value);
- return *this;
- }
-
- // check value type
- template <typename _Base>
- bool ValueBase<_Base>::is_undefined() const {
- return _Base::as_json() == 0;
- }
-
- template <typename _Base>
- bool ValueBase<_Base>::is_object() const {
- return json_is_object(_Base::as_json());
- }
-
- template <typename _Base>
- bool ValueBase<_Base>::is_array() const {
- return json_is_array(_Base::as_json());
- }
-
- template <typename _Base>
- bool ValueBase<_Base>::is_string() const {
- return json_is_string(_Base::as_json());
- }
-
- template <typename _Base>
- bool ValueBase<_Base>::is_integer() const {
- return json_is_integer(_Base::as_json());
- }
-
- template <typename _Base>
- bool ValueBase<_Base>::is_real() const {
- return json_is_real(_Base::as_json());
- }
-
- template <typename _Base>
- bool ValueBase<_Base>::is_number() const {
- return json_is_number(_Base::as_json());
- }
-
- template <typename _Base>
- bool ValueBase<_Base>::is_true() const {
- return json_is_true(_Base::as_json());
- }
-
- template <typename _Base>
- bool ValueBase<_Base>::is_false() const {
- return json_is_false(_Base::as_json());
- }
-
- template <typename _Base>
- bool ValueBase<_Base>::is_boolean() const {
- return json_is_boolean(_Base::as_json());
- }
-
- template <typename _Base>
- bool ValueBase<_Base>::is_null() const {
- return json_is_null(_Base::as_json());
- }
-
- // get size of array or object
- template <typename _Base>
- unsigned int 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 Value ValueBase<_Base>::at(unsigned int index) const {
- return Value(json_array_get(_Base::as_json(), index));
- }
-
- template <typename _Base>
- const Value ValueBase<_Base>::operator[](signed int index) const { return at(index); }
- template <typename _Base>
- const Value ValueBase<_Base>::operator[](unsigned int index) const { return at(index); }
- template <typename _Base>
- const Value ValueBase<_Base>::operator[](signed short index) const { return at(index); }
- template <typename _Base>
- const Value ValueBase<_Base>::operator[](unsigned short index) const { return at(index); }
- template <typename _Base>
- const Value ValueBase<_Base>::operator[](signed long index) const { return at(index); }
- template <typename _Base>
- const Value ValueBase<_Base>::operator[](unsigned long index) const { return at(index); }
-
- // get value at array index (non-const version)
- template <typename _Base>
- ValueBase<ElementProxy> ValueBase<_Base>::at(unsigned int index) {
- return ElementProxy(_Base::as_json(), index);
- }
-
- template <typename _Base>
- ValueBase<ElementProxy> ValueBase<_Base>::operator[](signed int index) {
- return at(index);
- }
-
- template <typename _Base>
- ValueBase<ElementProxy> ValueBase<_Base>::operator[](unsigned int index) {
- return at(index);
- }
-
- template <typename _Base>
- ValueBase<ElementProxy> ValueBase<_Base>::operator[](signed short index) {
- return at(index);
- }
-
- template <typename _Base>
- ValueBase<ElementProxy> ValueBase<_Base>::operator[](unsigned short index) {
- return at(index);
- }
-
- template <typename _Base>
- ValueBase<ElementProxy> ValueBase<_Base>::operator[](signed long index) {
- return at(index);
- }
-
- template <typename _Base>
- ValueBase<ElementProxy> ValueBase<_Base>::operator[](unsigned long index) {
- return at(index);
- }
-
- // get object property (const version)
- template <typename _Base>
- const Value ValueBase<_Base>::get(const char* key) const {
- return Value(json_object_get(_Base::as_json(), key));
- }
-
- template <typename _Base>
- const Value ValueBase<_Base>::get(const std::string& key) const {
- return get(key.c_str());
- }
-
- template <typename _Base>
- const Value ValueBase<_Base>::operator[](const char* key) const {
- return get(key);
- }
-
- template <typename _Base>
- const Value ValueBase<_Base>::operator[](const std::string& key) const {
- return get(key.c_str());
- }
-
- // get object property (non-const version)
- template <typename _Base>
- ValueBase<PropertyProxy> ValueBase<_Base>::get(const char* key) {
- return PropertyProxy(_Base::as_json(), key);
- }
-
- template <typename _Base>
- ValueBase<PropertyProxy> ValueBase<_Base>::get(const std::string& key) {
- return get(key.c_str());
- }
-
- template <typename _Base>
- ValueBase<PropertyProxy> ValueBase<_Base>::operator[](const char* key) {
- return get(key);
- }
-
- template <typename _Base>
- ValueBase<PropertyProxy> ValueBase<_Base>::operator[](const std::string& key) {
- return get(key.c_str());
- }
-
- // clear all array/object values
- template <typename _Base>
- void 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* ValueBase<_Base>::as_cstring() const {
- return json_string_value(_Base::as_json());
- }
-
- template <typename _Base>
- std::string ValueBase<_Base>::as_string() const {
- const char* tmp = as_cstring();
- return tmp == 0 ? "" : tmp;
- }
-
- template <typename _Base>
- int ValueBase<_Base>::as_integer() const {
- return json_integer_value(_Base::as_json());
- }
-
- template <typename _Base>
- double ValueBase<_Base>::as_real() const {
- return json_real_value(_Base::as_json());
- }
-
- template <typename _Base>
- double ValueBase<_Base>::as_number() const {
- return json_number_value(_Base::as_json());
- }
-
- template <typename _Base>
- bool ValueBase<_Base>::as_boolean() const {
- return is_true();
- }
-
- // set an object property (converts value to object is not one already)
- template <typename _Base>
- _Base& ValueBase<_Base>::set_key(const char* key, const Value& value) {
- json_object_set(_Base::as_json(), key, value._Base::as_json());
- return *this;
- }
-
- template <typename _Base>
- _Base& ValueBase<_Base>::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)
- template <typename _Base>
- _Base& ValueBase<_Base>::set_at(unsigned int index, const 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& ValueBase<_Base>::del_key(const char* key) {
- json_object_del(_Base::as_json(), key);
- return *this;
- }
-
- template <typename _Base>
- _Base& 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& 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& ValueBase<_Base>::insert_at(unsigned int index, const Value& value) {
- json_array_insert(_Base::as_json(), index, value._Base::as_json());
- return *this;
- }
-
- // write the value to a file
- template <typename _Base>
- int ValueBase<_Base>::save_file(const char* path, int flags) const {
- return json_dump_file(_Base::as_json(), path, flags);
- }
-
- // write the value to a string (caller must deallocate with free()!)
- template <typename _Base>
- char* ValueBase<_Base>::save_string(int flags) const {
- return json_dumps(_Base::as_json(), flags);
- }
-
- Basic::~Basic() {
- json_decref(_value);
- }
-
- // copy an existing Value
- Basic& Basic::operator=(const Basic& e) {
- if (&e != this) {
- json_decref(_value);
- _value = json_incref(e._value);
- }
- return *this;
- }
-
- // get the underlying json_t
- json_t* Basic::as_json() const {
- return _value;
- }
-
- // take ownership of a json_t (does not increase reference count)
- Basic Basic::take_ownership(json_t* json) {
- Basic v;
- v._value = json;
- return v;
- }
-
- // assign value to proxied array element
- ElementProxy& ElementProxy::operator=(const Value& value) {
- json_array_set(_array, _index, value.as_json());
- return *this;
- }
-
- // get the proxied element
- json_t* ElementProxy::as_json() const {
- return json_array_get(_array, _index);
- }
-
- // assign value to proxied object property
- PropertyProxy& PropertyProxy::operator=(const Value& value) {
- json_object_set(_object, _key, value.as_json());
- return *this;
- }
-
- json_t* PropertyProxy::as_json() const {
- return json_object_get(_object, _key);
- }
-
- } // namespace json::_private
-
- // construct Value::Value input
- Value::Value(const char* value) {
- _value = json_string(value);
- }
-
- Value::Value(const std::string& value) {
- _value = json_string(value.c_str());
- }
-
- Value::Value(bool value) {
- _value = value ? json_true() : json_false();
- }
-
- Value::Value(signed int value) {
- _value = json_integer(value);
- }
-
- Value::Value(unsigned int value) {
- _value = json_integer(value);
- }
-
- Value::Value(signed short value) {
- _value = json_integer(value);
- }
-
- Value::Value(unsigned short value) {
- _value = json_integer(value);
- }
-
- Value::Value(signed long value) {
- _value = json_integer(value);
- }
-
- Value::Value(unsigned long value) {
- _value = json_integer(value);
- }
-
- Value::Value(float value) {
- _value = json_real(value);
- }
-
- Value::Value(double value) {
- _value = json_real(value);
- }
-
- // construct a new iterator for a given object
- Iterator::Iterator(const Value& value) : _object(value), _iter(0) {
- _iter = json_object_iter(_object.as_json());
- }
-
- // 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());
- }
-
- // increment iterator
- void Iterator::next() {
- _iter = json_object_iter_next(_object.as_json(), _iter);
- }
-
- Iterator& Iterator::operator++() { next(); return *this; }
-
- // test if iterator is still valid
- bool Iterator::valid() const {
- return _iter != 0;
- }
-
- Iterator::operator bool() const {
- return valid();
- }
-
- // get key
- const char* Iterator::ckey() const {
- return json_object_iter_key(_iter);
- }
-
- std::string Iterator::key() const {
- return ckey();
- }
-
- // get value
- const Value Iterator::value() const {
- return Value(json_object_iter_value(_iter));
- }
-
- // dereference value
- const Value Iterator::operator*() const {
- return value();
- }
-
- // create a new empty object
- Value object() {
- return Value::take_ownership(json_object());
- }
-
- // create a new empty array
- Value array() {
- return Value::take_ownership(json_array());
- }
-
- // create a new null value
- Value null() {
- return Value::take_ownership(json_null());
- }
-
- // load a file as a JSON value
- Value load_file(const char* path, json_error_t* error) {
- return Value::take_ownership(json_load_file(path, error));
- }
-
- // load a string as a JSON value
- Value load_string(const char* string, json_error_t* error) {
- return Value::take_ownership(json_loads(string, error));
- }
+ namespace _private {
+ // assignment operator
+ template <typename _Base>
+ ValueBase<_Base>& ValueBase<_Base>::operator=(const Value& value) {
+ _Base::operator=(value);
+ return *this;
+ }
+
+ // check value type
+ template <typename _Base>
+ bool ValueBase<_Base>::is_undefined() const {
+ return _Base::as_json() == 0;
+ }
+
+ template <typename _Base>
+ bool ValueBase<_Base>::is_object() const {
+ return json_is_object(_Base::as_json());
+ }
+
+ template <typename _Base>
+ bool ValueBase<_Base>::is_array() const {
+ return json_is_array(_Base::as_json());
+ }
+
+ template <typename _Base>
+ bool ValueBase<_Base>::is_string() const {
+ return json_is_string(_Base::as_json());
+ }
+
+ template <typename _Base>
+ bool ValueBase<_Base>::is_integer() const {
+ return json_is_integer(_Base::as_json());
+ }
+
+ template <typename _Base>
+ bool ValueBase<_Base>::is_real() const {
+ return json_is_real(_Base::as_json());
+ }
+
+ template <typename _Base>
+ bool ValueBase<_Base>::is_number() const {
+ return json_is_number(_Base::as_json());
+ }
+
+ template <typename _Base>
+ bool ValueBase<_Base>::is_true() const {
+ return json_is_true(_Base::as_json());
+ }
+
+ template <typename _Base>
+ bool ValueBase<_Base>::is_false() const {
+ return json_is_false(_Base::as_json());
+ }
+
+ template <typename _Base>
+ bool ValueBase<_Base>::is_boolean() const {
+ return json_is_boolean(_Base::as_json());
+ }
+
+ template <typename _Base>
+ bool ValueBase<_Base>::is_null() const {
+ return json_is_null(_Base::as_json());
+ }
+
+ // get size of array or object
+ template <typename _Base>
+ unsigned int 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 Value ValueBase<_Base>::at(unsigned int index) const {
+ return Value(json_array_get(_Base::as_json(), index));
+ }
+
+ template <typename _Base>
+ const Value ValueBase<_Base>::operator[](signed int index) const { return at(index); }
+ template <typename _Base>
+ const Value ValueBase<_Base>::operator[](unsigned int index) const { return at(index); }
+ template <typename _Base>
+ const Value ValueBase<_Base>::operator[](signed short index) const { return at(index); }
+ template <typename _Base>
+ const Value ValueBase<_Base>::operator[](unsigned short index) const { return at(index); }
+ template <typename _Base>
+ const Value ValueBase<_Base>::operator[](signed long index) const { return at(index); }
+ template <typename _Base>
+ const Value ValueBase<_Base>::operator[](unsigned long index) const { return at(index); }
+
+ // get value at array index (non-const version)
+ template <typename _Base>
+ ValueBase<ElementProxy> ValueBase<_Base>::at(unsigned int index) {
+ return ElementProxy(_Base::as_json(), index);
+ }
+
+ template <typename _Base>
+ ValueBase<ElementProxy> ValueBase<_Base>::operator[](signed int index) {
+ return at(index);
+ }
+
+ template <typename _Base>
+ ValueBase<ElementProxy> ValueBase<_Base>::operator[](unsigned int index) {
+ return at(index);
+ }
+
+ template <typename _Base>
+ ValueBase<ElementProxy> ValueBase<_Base>::operator[](signed short index) {
+ return at(index);
+ }
+
+ template <typename _Base>
+ ValueBase<ElementProxy> ValueBase<_Base>::operator[](unsigned short index) {
+ return at(index);
+ }
+
+ template <typename _Base>
+ ValueBase<ElementProxy> ValueBase<_Base>::operator[](signed long index) {
+ return at(index);
+ }
+
+ template <typename _Base>
+ ValueBase<ElementProxy> ValueBase<_Base>::operator[](unsigned long index) {
+ return at(index);
+ }
+
+ // get object property (const version)
+ template <typename _Base>
+ const Value ValueBase<_Base>::get(const char* key) const {
+ return Value(json_object_get(_Base::as_json(), key));
+ }
+
+ template <typename _Base>
+ const Value ValueBase<_Base>::get(const std::string& key) const {
+ return get(key.c_str());
+ }
+
+ template <typename _Base>
+ const Value ValueBase<_Base>::operator[](const char* key) const {
+ return get(key);
+ }
+
+ template <typename _Base>
+ const Value ValueBase<_Base>::operator[](const std::string& key) const {
+ return get(key.c_str());
+ }
+
+ // get object property (non-const version)
+ template <typename _Base>
+ ValueBase<PropertyProxy> ValueBase<_Base>::get(const char* key) {
+ return PropertyProxy(_Base::as_json(), key);
+ }
+
+ template <typename _Base>
+ ValueBase<PropertyProxy> ValueBase<_Base>::get(const std::string& key) {
+ return get(key.c_str());
+ }
+
+ template <typename _Base>
+ ValueBase<PropertyProxy> ValueBase<_Base>::operator[](const char* key) {
+ return get(key);
+ }
+
+ template <typename _Base>
+ ValueBase<PropertyProxy> ValueBase<_Base>::operator[](const std::string& key) {
+ return get(key.c_str());
+ }
+
+ // clear all array/object values
+ template <typename _Base>
+ void 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* ValueBase<_Base>::as_cstring() const {
+ return json_string_value(_Base::as_json());
+ }
+
+ template <typename _Base>
+ std::string ValueBase<_Base>::as_string() const {
+ const char* tmp = as_cstring();
+ return tmp == 0 ? "" : tmp;
+ }
+
+ template <typename _Base>
+ int ValueBase<_Base>::as_integer() const {
+ return json_integer_value(_Base::as_json());
+ }
+
+ template <typename _Base>
+ double ValueBase<_Base>::as_real() const {
+ return json_real_value(_Base::as_json());
+ }
+
+ template <typename _Base>
+ double ValueBase<_Base>::as_number() const {
+ return json_number_value(_Base::as_json());
+ }
+
+ template <typename _Base>
+ bool ValueBase<_Base>::as_boolean() const {
+ return is_true();
+ }
+
+ // set an object property (converts value to object is not one already)
+ template <typename _Base>
+ _Base& ValueBase<_Base>::set_key(const char* key, const Value& value) {
+ json_object_set(_Base::as_json(), key, value._Base::as_json());
+ return *this;
+ }
+
+ template <typename _Base>
+ _Base& ValueBase<_Base>::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)
+ template <typename _Base>
+ _Base& ValueBase<_Base>::set_at(unsigned int index, const 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& ValueBase<_Base>::del_key(const char* key) {
+ json_object_del(_Base::as_json(), key);
+ return *this;
+ }
+
+ template <typename _Base>
+ _Base& 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& 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& ValueBase<_Base>::insert_at(unsigned int index, const Value& value) {
+ json_array_insert(_Base::as_json(), index, value._Base::as_json());
+ return *this;
+ }
+
+ // write the value to a file
+ template <typename _Base>
+ int ValueBase<_Base>::save_file(const char* path, int flags) const {
+ return json_dump_file(_Base::as_json(), path, flags);
+ }
+
+ // write the value to a string (caller must deallocate with free()!)
+ template <typename _Base>
+ char* ValueBase<_Base>::save_string(int flags) const {
+ return json_dumps(_Base::as_json(), flags);
+ }
+
+ Basic::~Basic() {
+ json_decref(_value);
+ }
+
+ // copy an existing Value
+ Basic& Basic::operator=(const Basic& e) {
+ if (&e != this) {
+ json_decref(_value);
+ _value = json_incref(e._value);
+ }
+ return *this;
+ }
+
+ // get the underlying json_t
+ json_t* Basic::as_json() const {
+ return _value;
+ }
+
+ // take ownership of a json_t (does not increase reference count)
+ Basic Basic::take_ownership(json_t* json) {
+ Basic v;
+ v._value = json;
+ return v;
+ }
+
+ // assign value to proxied array element
+ ElementProxy& ElementProxy::operator=(const Value& value) {
+ json_array_set(_array, _index, value.as_json());
+ return *this;
+ }
+
+ // get the proxied element
+ json_t* ElementProxy::as_json() const {
+ return json_array_get(_array, _index);
+ }
+
+ // assign value to proxied object property
+ PropertyProxy& PropertyProxy::operator=(const Value& value) {
+ json_object_set(_object, _key, value.as_json());
+ return *this;
+ }
+
+ json_t* PropertyProxy::as_json() const {
+ return json_object_get(_object, _key);
+ }
+
+ } // namespace json::_private
+
+ // construct Value::Value input
+ Value::Value(const char* value) {
+ _value = json_string(value);
+ }
+
+ Value::Value(const std::string& value) {
+ _value = json_string(value.c_str());
+ }
+
+ Value::Value(bool value) {
+ _value = value ? json_true() : json_false();
+ }
+
+ Value::Value(signed int value) {
+ _value = json_integer(value);
+ }
+
+ Value::Value(unsigned int value) {
+ _value = json_integer(value);
+ }
+
+ Value::Value(signed short value) {
+ _value = json_integer(value);
+ }
+
+ Value::Value(unsigned short value) {
+ _value = json_integer(value);
+ }
+
+ Value::Value(signed long value) {
+ _value = json_integer(value);
+ }
+
+ Value::Value(unsigned long value) {
+ _value = json_integer(value);
+ }
+
+ Value::Value(float value) {
+ _value = json_real(value);
+ }
+
+ Value::Value(double value) {
+ _value = json_real(value);
+ }
+
+ // construct a new iterator for a given object
+ Iterator::Iterator(const Value& value) : _object(value), _iter(0) {
+ _iter = json_object_iter(_object.as_json());
+ }
+
+ // 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());
+ }
+
+ // increment iterator
+ void Iterator::next() {
+ _iter = json_object_iter_next(_object.as_json(), _iter);
+ }
+
+ Iterator& Iterator::operator++() { next(); return *this; }
+
+ // test if iterator is still valid
+ bool Iterator::valid() const {
+ return _iter != 0;
+ }
+
+ Iterator::operator bool() const {
+ return valid();
+ }
+
+ // get key
+ const char* Iterator::ckey() const {
+ return json_object_iter_key(_iter);
+ }
+
+ std::string Iterator::key() const {
+ return ckey();
+ }
+
+ // get value
+ const Value Iterator::value() const {
+ return Value(json_object_iter_value(_iter));
+ }
+
+ // dereference value
+ const Value Iterator::operator*() const {
+ return value();
+ }
+
+ // create a new empty object
+ Value object() {
+ return Value::take_ownership(json_object());
+ }
+
+ // create a new empty array
+ Value array() {
+ return Value::take_ownership(json_array());
+ }
+
+ // create a new null value
+ Value null() {
+ return Value::take_ownership(json_null());
+ }
+
+ // load a file as a JSON value
+ Value load_file(const char* path, json_error_t* error) {
+ return Value::take_ownership(json_load_file(path, error));
+ }
+
+ // load a string as a JSON value
+ Value load_string(const char* string, json_error_t* error) {
+ return Value::take_ownership(json_loads(string, error));
+ }
} // namespace json
// stream JSON value out
std::ostream& operator<<(std::ostream& os, const json::Value& value) {
- // get the temporary serialize string
- char* tmp = value.save_string();
- if (tmp != 0) {
- // stream temp string out and release it
- os << tmp;
- free(tmp);
- }
- return os;
+ // get the temporary serialize string
+ char* tmp = value.save_string();
+ if (tmp != 0) {
+ // stream temp string out and release it
+ os << tmp;
+ free(tmp);
+ }
+ return os;
}
// read JSON value
std::istream& operator>>(std::istream& is, json::Value& value) {
- // buffer the remaining bytes into a single string for Jansson
- std::stringstream tmp;
- while (is)
- tmp << static_cast<char>(is.get());
- // parse the buffered string
- value = json::load_string(tmp.str().c_str());
- return is;
+ // buffer the remaining bytes into a single string for Jansson
+ std::stringstream tmp;
+ while (is)
+ tmp << static_cast<char>(is.get());
+ // parse the buffered string
+ value = json::load_string(tmp.str().c_str());
+ return is;
}
diff --git a/src/jansson.hpp b/src/jansson.hpp
index 1bd18b1..020a277 100644
--- a/src/jansson.hpp
+++ b/src/jansson.hpp
@@ -13,281 +13,281 @@
#include <cstdlib>
namespace json {
- // include Jansson C library into the json namespace
- #include <jansson.h>
-
- class Iterator;
- class Value;
-
- // implementation details; do not use directly
- namespace _private {
- class ElementProxy;
- class PropertyProxy;
+ // include Jansson C library into the json namespace
+# include <jansson.h>
+
+ class Iterator;
+ class Value;
+
+ // implementation details; do not use directly
+ namespace _private {
+ class ElementProxy;
+ class PropertyProxy;
- // base class for JSON value interface
- 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) {}
-
- // assignment operator
- inline ValueBase& operator=(const Value& value);
+ // base class for JSON value interface
+ 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) {}
+
+ // assignment operator
+ inline ValueBase& operator=(const Value& value);
- // check value type
- inline bool is_undefined() const;
- inline bool is_object() const;
- inline bool is_array() const;
- inline bool is_string() const;
- inline bool is_integer() const;
- inline bool is_real() const;
- inline bool is_number() const;
- inline bool is_true() const;
- inline bool is_false() const;
- inline bool is_boolean() const;
- inline bool is_null() const;
+ // check value type
+ inline bool is_undefined() const;
+ inline bool is_object() const;
+ inline bool is_array() const;
+ inline bool is_string() const;
+ inline bool is_integer() const;
+ inline bool is_real() const;
+ inline bool is_number() const;
+ inline bool is_true() const;
+ inline bool is_false() const;
+ inline bool is_boolean() const;
+ inline bool is_null() const;
- // get size of array or object
- inline unsigned int size() const;
+ // 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;
+ // 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;
+ 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 ValueBase<ElementProxy> at(unsigned int index);
+ // get value at array index (non-const version)
+ inline ValueBase<ElementProxy> at(unsigned int index);
- inline ValueBase<ElementProxy> operator[](signed int index);
- inline ValueBase<ElementProxy> operator[](unsigned int index);
- inline ValueBase<ElementProxy> operator[](signed short index);
- inline ValueBase<ElementProxy> operator[](unsigned short index);
- inline ValueBase<ElementProxy> operator[](signed long index);
- inline ValueBase<ElementProxy> operator[](unsigned long index);
+ inline ValueBase<ElementProxy> operator[](signed int index);
+ inline ValueBase<ElementProxy> operator[](unsigned int index);
+ inline ValueBase<ElementProxy> operator[](signed short index);
+ inline ValueBase<ElementProxy> operator[](unsigned short index);
+ inline ValueBase<ElementProxy> operator[](signed long index);
+ inline ValueBase<ElementProxy> operator[](unsigned long index);
- // get object property (const version)
- inline const Value get(const char* key) const;
+ // get object property (const version)
+ 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;
+ 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;
- // get object property (non-const version)
- inline ValueBase<PropertyProxy> get(const char* key);
+ // get object property (non-const version)
+ inline ValueBase<PropertyProxy> get(const char* key);
- inline ValueBase<PropertyProxy> get(const std::string& key);
- inline ValueBase<PropertyProxy> operator[](const char* key);
- inline ValueBase<PropertyProxy> operator[](const std::string& key);
+ inline ValueBase<PropertyProxy> get(const std::string& key);
+ inline ValueBase<PropertyProxy> operator[](const char* key);
+ inline ValueBase<PropertyProxy> operator[](const std::string& key);
- // clear all array/object values
- inline void clear();
+ // 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;
+ // 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);
+ // 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);
-
- // write the value to a file
- inline int save_file(const char* path, int flags = 0) const;
-
- // write the value to a string (caller must deallocate with free()!)
- inline char* save_string(int flags = 0) const;
- };
-
- // represents any JSON value, private base
- class Basic {
- public:
- // construct new Value with an undefined value
- Basic() : _value(0) {}
-
- // copy constructor
- Basic(const Basic& value) : _value(json_incref(value._value)) {}
-
- // make a reference to an existing json_t value
- explicit Basic(json_t* value) : _value(json_incref(value)) {}
-
- // free Value resources
- inline ~Basic();
-
- // copy an existing Value
- inline Basic& operator=(const Basic& e);
-
- // get the underlying json_t
- inline json_t* as_json() const;
+ 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);
+
+ // write the value to a file
+ inline int save_file(const char* path, int flags = 0) const;
+
+ // write the value to a string (caller must deallocate with free()!)
+ inline char* save_string(int flags = 0) const;
+ };
+
+ // represents any JSON value, private base
+ class Basic {
+ public:
+ // construct new Value with an undefined value
+ Basic() : _value(0) {}
+
+ // copy constructor
+ Basic(const Basic& value) : _value(json_incref(value._value)) {}
+
+ // make a reference to an existing json_t value
+ explicit Basic(json_t* value) : _value(json_incref(value)) {}
+
+ // free Value resources
+ inline ~Basic();
+
+ // copy an existing Value
+ inline Basic& operator=(const Basic& e);
- // take ownership of a json_t (does not increase reference count)
- inline static Basic take_ownership(json_t* json);
+ // get the underlying json_t
+ inline json_t* as_json() const;
- protected:
- // internal value pointer
- json_t* _value;
- };
+ // take ownership of a json_t (does not increase reference count)
+ inline static Basic take_ownership(json_t* json);
- // proxies an array element
- class ElementProxy {
- public:
- // constructor
- ElementProxy(json_t* array, unsigned int index) : _array(array), _index(index) {}
+ protected:
+ // internal value pointer
+ json_t* _value;
+ };
- // assign to the proxied element
- inline ElementProxy& operator=(const Value& value);
+ // proxies an array element
+ class ElementProxy {
+ public:
+ // constructor
+ ElementProxy(json_t* array, unsigned int index) : _array(array), _index(index) {}
- // get the proxied element
- inline json_t* as_json() const;
+ // assign to the proxied element
+ inline ElementProxy& operator=(const Value& value);
- private:
- // array object we wrap
- json_t* _array;
+ // get the proxied element
+ inline json_t* as_json() const;
- // index of property
- unsigned int _index;
- };
+ private:
+ // array object we wrap
+ json_t* _array;
- // proxies an object property
- class PropertyProxy {
- public:
- // constructor
- PropertyProxy(json_t* array, const char* key) : _object(array), _key(key) {}
+ // index of property
+ unsigned int _index;
+ };
- // assign to the proxied element
- inline PropertyProxy& operator=(const Value& value);
+ // proxies an object property
+ class PropertyProxy {
+ public:
+ // constructor
+ PropertyProxy(json_t* array, const char* key) : _object(array), _key(key) {}
- // get the proxied element
- inline json_t* as_json() const;
+ // assign to the proxied element
+ inline PropertyProxy& operator=(const Value& value);
- private:
- // array object we wrap
- json_t* _object;
+ // get the proxied element
+ inline json_t* as_json() const;
- // key of property
- const char* _key;
- };
+ private:
+ // array object we wrap
+ json_t* _object;
- } // namespace json::_private
+ // key of property
+ const char* _key;
+ };
- // represents any JSON value
- class Value : public _private::ValueBase<_private::Basic> {
- public:
- // construct Value from input
- explicit inline Value(const char* value);
- explicit inline Value(const std::string& value);
- explicit inline Value(bool value);
- explicit inline Value(signed int value);
- explicit inline Value(unsigned int value);
- explicit inline Value(signed short value);
- explicit inline Value(unsigned short value);
- explicit inline Value(signed long value);
- explicit inline Value(unsigned long value);
- explicit inline Value(float value);
- explicit inline Value(double value);
+ } // namespace json::_private
- // empty constructor
- Value() : _private::ValueBase<_private::Basic>() {}
+ // represents any JSON value
+ class Value : public _private::ValueBase<_private::Basic> {
+ public:
+ // construct Value from input
+ explicit inline Value(const char* value);
+ explicit inline Value(const std::string& value);
+ explicit inline Value(bool value);
+ explicit inline Value(signed int value);
+ explicit inline Value(unsigned int value);
+ explicit inline Value(signed short value);
+ explicit inline Value(unsigned short value);
+ explicit inline Value(signed long value);
+ explicit inline Value(unsigned long value);
+ explicit inline Value(float value);
+ explicit inline Value(double value);
- // copy constructor for base
- Value(const _private::Basic& value) : _private::ValueBase<_private::Basic>(value) {}
-
- // copy constructor for base
- Value(const _private::ValueBase<_private::Basic>& value) : _private::ValueBase<_private::Basic>(value) {}
+ // empty constructor
+ Value() : _private::ValueBase<_private::Basic>() {}
- // copy constructor
- Value(const Value& value) : _private::ValueBase<_private::Basic>(value) {}
+ // copy constructor for base
+ Value(const _private::Basic& value) : _private::ValueBase<_private::Basic>(value) {}
- // create reference to value
- explicit Value(json_t* json) : _private::ValueBase<_private::Basic>(json) {}
- };
+ // copy constructor for base
+ Value(const _private::ValueBase<_private::Basic>& value) : _private::ValueBase<_private::Basic>(value) {}
- // iterators over a JSON object
- class Iterator {
- public:
- // construct a new iterator for a given object
- inline Iterator(const Value& value);
+ // copy constructor
+ Value(const Value& value) : _private::ValueBase<_private::Basic>(value) {}
- // construct a new iterator for a given object
- inline Iterator(const _private::ValueBase<_private::PropertyProxy>& value);
+ // create reference to value
+ explicit Value(json_t* json) : _private::ValueBase<_private::Basic>(json) {}
+ };
- // increment iterator
- inline void next();
+ // iterators over a JSON object
+ class Iterator {
+ public:
+ // construct a new iterator for a given object
+ inline Iterator(const Value& value);
- inline Iterator& operator++();
+ // construct a new iterator for a given object
+ inline Iterator(const _private::ValueBase<_private::PropertyProxy>& value);
- // test if iterator is still valid
- inline bool valid() const;
+ // increment iterator
+ inline void next();
- inline operator bool() const;
+ inline Iterator& operator++();
- // get key
- inline const char* ckey() const;
+ // test if iterator is still valid
+ inline bool valid() const;
- inline std::string key() const;
+ inline operator bool() const;
- // get value
- inline const Value value() const;
+ // get key
+ inline const char* ckey() const;
- // dereference value
- inline const Value operator*() const;
+ inline std::string key() const;
- private:
- // disallow copying
- Iterator(const Iterator&);
- Iterator& operator=(const Iterator&);
+ // get value
+ inline const Value value() const;
- // object being iterated over
- Value _object;
+ // dereference value
+ inline const Value operator*() const;
- // iterator value
- void* _iter;
- };
+ private:
+ // disallow copying
+ Iterator(const Iterator&);
+ Iterator& operator=(const Iterator&);
- // create a new empty object
- inline Value object();
+ // object being iterated over
+ Value _object;
- // create a new empty array
- inline Value array();
+ // iterator value
+ void* _iter;
+ };
- // create a new null value
- inline Value null();
+ // create a new empty object
+ inline Value object();
- // load a file as a JSON value
- inline Value load_file(const char* path, json_error_t* error = 0);
+ // create a new empty array
+ inline Value array();
- // load a string as a JSON value
- inline Value load_string(const char* string, json_error_t* error = 0);
+ // create a new null value
+ inline Value null();
-} // namespace json
+ // 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
inline std::ostream& operator<<(std::ostream& os, const json::Value& value);