+// janssonxx - C++ wrapper for jansson
+//
+// author: Sean Middleditch <sean@middleditch.us>
+//
+// janssonxx is free software; you can redistribute it and/or modify
+// it under the terms of the MIT license. See LICENSE for details.
+
+
#if !defined(JANSSONXX_H)
#define JANSSONXX_H 1
#include <string>
-#include <jansson.h>
+#include <ostream>
+#include <istream>
+#include <sstream>
+#include <cstdlib>
namespace jansson {
+// include in the jansson namespace
+#include <jansson.h>
+
class Iterator;
// represents any JSON value
// copy an existing Value
Value& operator=(const Value& e) {
- json_decref(_value);
- _value = json_incref(e._value);
- return *this;
- }
-
- // take ownership of a json_t (does not increase reference count)
- Value& take_ownership(json_t* value) {
- json_decref(_value);
- _value = value;
+ if (&e != this) {
+ json_decref(_value);
+ _value = json_incref(e._value);
+ }
return *this;
}
// load a file as a JSON value
static Value load_file(const char* path, json_error_t* error = 0) {
- return Value().take_ownership(json_load_file(path, error));
+ return Value::_take(json_load_file(path, error));
}
// load a string as a JSON value
static Value load_string(const char* string, json_error_t* error = 0) {
- return Value().take_ownership(json_loads(string, error));
+ return Value::_take(json_loads(string, error));
}
+ // write the value to a file
+ int save_file(const char* path, int flags = JSON_INDENT(2)) const {
+ return json_dump_file(_value, path, flags);
+ }
+
+ // write the value to a string (caller must deallocate with free()!)
+ char* save_string(int flags = JSON_INDENT(2)) const {
+ return json_dumps(_value, flags);
+ }
+
+ // construct Value from input
+ static Value from(const char* value) { return Value::_take(json_string(value)); }
+ static Value from(const std::string& value) { return from(value.c_str()); }
+ static Value from(bool value) { return Value::_take(value ? json_true() : json_false()); }
+ static Value from(int value) { return Value::_take(json_integer(value)); }
+ static Value from(double value) { return Value::_take(json_real(value)); }
+
+ // create a new empty object
+ static Value object() { return Value::_take(json_object()); }
+
+ // create a new empty array
+ static Value array() { return Value::_take(json_array()); }
+
+ // create a new null value
+ static Value null() { return Value::_take(json_null()); }
+
// get the underlying json_t
- json_t* as_json_t() const { return _value; }
+ json_t* as_json() const { return _value; }
- // check Value value type
+ // check value type
bool is_undefined() const { return _value == 0; }
bool is_object() const { return json_is_object(_value); }
bool is_array() const { return json_is_array(_value); }
unsigned int size() const {
if (is_object())
return json_object_size(_value);
- else if (is_array())
- return json_array_size(_value);
else
- return 0;
+ return json_array_size(_value);
}
- // get value at array index
+ // get value at array index (const version)
const Value at(unsigned int index) const {
- if (is_array())
- return Value(json_array_get(_value, index));
- else
- return Value();
+ return Value(json_array_get(_value, index));
}
const Value operator[](signed int index) const { return at(index); }
const Value operator[](signed long index) const { return at(index); }
const Value operator[](unsigned long index) const { return at(index); }
+ // get value at array index (non-const version)
+ Value at(unsigned int index) {
+ return Value(json_array_get(_value, index));
+ }
+
+ Value operator[](signed int index) { return at(index); }
+ Value operator[](unsigned int index) { return at(index); }
+ Value operator[](signed short index) { return at(index); }
+ Value operator[](unsigned short index) { return at(index); }
+ Value operator[](signed long index) { return at(index); }
+ Value operator[](unsigned long index) { return at(index); }
+
// get object property
const Value get(const char* key) const {
- if (is_object())
- return Value(json_object_get(_value, key));
- else
- return Value();
+ return Value(json_object_get(_value, key));
}
const Value get(const std::string& key) const { return get(key.c_str()); }
void clear() {
if (is_object())
json_object_clear(_value);
- else if (is_array())
+ else
json_array_clear(_value);
}
double as_number() const { return json_number_value(_value); }
bool as_boolean() const { return is_true(); }
- // assign new string value
- Value& operator=(const char* value) {
- json_decref(_value);
- _value = json_string(value);
+ // set an object property (converts value to object is not one already)
+ Value& set_key(const char* key, const Value& value) {
+ json_object_set(_value, key, value.as_json());
return *this;
}
- // assign new integer value
- Value& operator=(int value) {
- json_decref(_value);
- _value = json_integer(value);
- return *this;
+ Value& set_key(const std::string& key, const Value& value) {
+ return set_key(key.c_str(), value);
}
- // assign new real/double/number value
- Value& operator=(double value) {
- json_decref(_value);
- _value = json_real(value);
+ // set an array index (converts value to object is not one already)
+ Value& set_at(unsigned int index, const Value& value) {
+ if (index == size())
+ json_array_append(_value, value.as_json());
+ else
+ json_array_set(_value, index, value.as_json());
return *this;
}
- // assign new boolean value
- Value& operator=(bool value) {
- json_decref(_value);
- _value = value ? json_true() : json_false();
- return *this;
+ Value& set_at(int index, const Value& value) {
+ return set_at(static_cast<unsigned int>(index), value);
}
private:
+ // take ownership of a json_t (does not increase reference count)
+ static Value _take(json_t* json) {
+ Value v;
+ v._value = json;
+ return v;
+ }
+
// internal value pointer
json_t* _value;
};
public:
// construct a new iterator for a given object
Iterator(const Value& value) : _object(value), _iter(0) {
- _iter = json_object_iter(_object.as_json_t());
+ _iter = json_object_iter(_object.as_json());
}
// increment iterator
void next() {
- if (_iter != 0)
- _iter = json_object_iter_next(_object.as_json_t(), _iter);
+ _iter = json_object_iter_next(_object.as_json(), _iter);
}
Iterator& operator++() { next(); return *this; }
// get key
const char* ckey() const {
- if (_iter != 0)
- return json_object_iter_key(_iter);
- else
- return "";
+ return json_object_iter_key(_iter);
}
std::string key() const { return ckey(); }
// get value
const Value value() const {
- if (_iter != 0)
- return Value(json_object_iter_value(_iter));
- else
- return Value();
+ return Value(json_object_iter_value(_iter));
}
// dereference value
} // namespace jansson
+// stream JSON value out
+std::ostream& operator<<(std::ostream& os, const jansson::Value& value) {
+ char* tmp = value.save_string();
+ os << tmp;
+ free(tmp);
+ return os;
+}
+
+// read JSON value
+std::istream& operator>>(std::istream& is, jansson::Value& value) {
+ std::stringstream tmp;
+ while (is)
+ tmp << static_cast<char>(is.get());
+ value = jansson::Value::load_string(tmp.str().c_str());
+ return is;
+}
+
#endif