add proper attribution to janssonxx.h
[jansson.git] / janssonxx.h
index 6e81241..6039646 100644 (file)
@@ -1,11 +1,25 @@
+// 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
@@ -25,46 +39,51 @@ public:
 
        // 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_ownership(json_string(value)); }
+       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_ownership(value ? json_true() : json_false()); }
-       static Value from(int value) { return Value().take_ownership(json_integer(value)); }
-       static Value from(double value) { return Value().take_ownership(json_real(value)); }
+       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_ownership(json_object()); }
+       static Value object() { return Value::_take(json_object()); }
 
        // create a new empty array
-       static Value array() { return Value().take_ownership(json_array()); }
+       static Value array() { return Value::_take(json_array()); }
 
        // create a new null value
-       static Value null() { return Value().take_ownership(json_null()); }
+       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 type
        bool is_undefined() const { return _value == 0; }
@@ -83,18 +102,13 @@ public:
        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 (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); }
@@ -106,10 +120,7 @@ public:
 
        // get value at array index (non-const version)
        Value at(unsigned int index) {
-               if (is_array())
-                       return Value(json_array_get(_value, index));
-               else
-                       return Value();
+               return Value(json_array_get(_value, index));
        }
 
        Value operator[](signed int index) { return at(index); }
@@ -121,10 +132,7 @@ public:
 
        // 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()); }
@@ -135,7 +143,7 @@ public:
        void clear() {
                if (is_object())
                        json_object_clear(_value);
-               else if (is_array())
+               else
                        json_array_clear(_value);
        }
 
@@ -148,35 +156,36 @@ public:
        bool as_boolean() const { return is_true(); }
 
        // set an object property (converts value to object is not one already)
-       Value& set(const char* key, const Value& value) {
-               if (!is_object()) {
-                       json_decref(_value);
-                       _value = json_object();
-               }
-
-               json_object_set(_value, key, value.as_json_t());
-
+       Value& set_key(const char* key, const Value& value) {
+               json_object_set(_value, key, value.as_json());
                return *this;
        }
 
-       // set an array index (converts value to object is not one already)
-       Value& set(unsigned int index, const Value& value) {
-               if (!is_array()) {
-                       json_decref(_value);
-                       _value = json_array();
-               }
+       Value& set_key(const std::string& key, const Value& value) {
+               return set_key(key.c_str(), value);
+       }
 
+       // set an array index (converts value to object is not one already)
+       Value& set_at(unsigned int index, const Value& value) {
                if (index == size())
-                       json_array_append(_value, value.as_json_t());
+                       json_array_append(_value, value.as_json());
                else
-                       json_array_set(_value, index, value.as_json_t());
-
+                       json_array_set(_value, index, value.as_json());
                return *this;
        }
 
-       Value& set(int index, const Value& value) { return set(static_cast<unsigned int>(index), value); }
+       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;
 };
@@ -186,13 +195,12 @@ class Iterator {
 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; }
@@ -203,20 +211,14 @@ public:
 
        // 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
@@ -236,4 +238,21 @@ private:
 
 } // 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