Merge branch '1.2'
[jansson.git] / src / jansson.ipp
index a845e84..e965ef7 100644 (file)
@@ -1,4 +1,5 @@
 // Copyright (c) 2010 Sean Middleditch <sean@middleditch.us>
+// Copyright (c) 2010 Petri Lehtinen <petri@digip.org>
 //
 // Jansson is free software; you can redistribute it and/or modify
 // it under the terms of the MIT license. See LICENSE for details.
@@ -7,6 +8,8 @@
 #error "jansson.ipp may only be included from jansson.hpp"
 #endif
 
+#include <string.h>
+
 namespace json {
     namespace detail {
         // assignment operator
@@ -269,13 +272,18 @@ namespace json {
 
         // write the value to a file
         template <typename _Base>
-        int ValueBase<_Base>::save_file(const char* path, int flags) const {
+        int ValueBase<_Base>::dump_file(const char* path, int flags) const {
             return json_dump_file(_Base::as_json(), path, flags);
         }
 
+        template <typename _Base>
+        int ValueBase<_Base>::dump_file(const std::string& path, int flags) const {
+            return dump_file(path.c_str(), flags);
+        }
+
         // write the value to a string (caller must deallocate with free()!)
         template <typename _Base>
-        char* ValueBase<_Base>::save_string(int flags) const {
+        char* ValueBase<_Base>::dumps(int flags) const {
             return json_dumps(_Base::as_json(), flags);
         }
 
@@ -304,6 +312,20 @@ namespace json {
             return v;
         }
 
+        ElementProxy::ElementProxy(json_t* array, unsigned int index)
+            : _array(array), _index(index) {
+            json_incref(_array);
+        }
+
+        ElementProxy::ElementProxy(const ElementProxy& other) 
+            : _array(other._array), _index(other._index) {
+            json_incref(_array);
+        }
+
+        ElementProxy::~ElementProxy() {
+            json_decref(_array);
+        }
+
         // assign value to proxied array element
         ElementProxy& ElementProxy::operator=(const Value& value) {
             json_array_set(_array, _index, value.as_json());
@@ -315,14 +337,40 @@ namespace json {
             return json_array_get(_array, _index);
         }
 
+        PropertyProxy::PropertyProxy(json_t* object, const char* key)
+            : _object(object), _key(0) {
+            _iter = json_object_iter_at(object, key);
+            if(!_iter)
+                _key = strdup(key);
+            json_incref(_object);
+        }
+
+        PropertyProxy::PropertyProxy(const PropertyProxy& other)
+            : _object(other._object), _iter(other._iter), _key(0) {
+            if(other._key)
+                _key = strdup(other._key);
+            json_incref(_object);
+        }
+
+        PropertyProxy::~PropertyProxy() {
+            free(_key);
+            json_decref(_object);
+        }
+
         // assign value to proxied object property
         PropertyProxy& PropertyProxy::operator=(const Value& value) {
-            json_object_set(_object, _key, value.as_json());
+            if(_iter)
+                json_object_iter_set(_object, _iter, value.as_json());
+            else
+                json_object_set(_object, _key, value.as_json());
             return *this;
         }
 
         json_t* PropertyProxy::as_json() const {
-            return json_object_get(_object, _key);
+            if(_iter)
+                return json_object_iter_value(_iter);
+            else
+                return json_object_get(_object, _key);
         }
 
     } // namespace json::detail
@@ -438,17 +486,25 @@ namespace json {
         return Value::take_ownership(json_load_file(path, error));
     }
 
+    Value load_file(const std::string& path, json_error_t* error) {
+        return load_file(path.c_str(), error);
+    }
+
     // load a string as a JSON value
-    Value load_string(const char* string, json_error_t* error) {
+    Value loads(const char* string, json_error_t* error) {
         return Value::take_ownership(json_loads(string, error));
     }
 
+    Value loads(const std::string& string, json_error_t* error) {
+        return loads(string.c_str(), 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();
+    char* tmp = value.dumps();
     if (tmp != 0) {
         // stream temp string out and release it
         os << tmp;
@@ -464,6 +520,6 @@ std::istream& operator>>(std::istream& is, json::Value& value) {
     while (is)
         tmp << static_cast<char>(is.get());
     // parse the buffered string
-    value = json::load_string(tmp.str().c_str());
+    value = json::loads(tmp.str().c_str());
     return is;
 }