add object property proxy support
[jansson.git] / janssonxx.h
index 935ac9e..2193ff1 100644 (file)
@@ -21,6 +21,8 @@ namespace jansson {
 
        class Iterator;
        class Value;
+       class _ArrayProxy;
+       class _ObjectProxy;
 
        // base class for JSON value interface
        template <typename _Base>
@@ -35,6 +37,9 @@ namespace jansson {
                // create reference to value
                _ValueBase(json_t* json) : _Base(json) {}
 
+               // assignment operator
+               _ValueBase& operator=(const Value& value) { _Base::operator=(value); return *this; }
+
                // check value type
                bool is_undefined() const { return _Base::as_json() == 0; }
                bool is_object() const { return json_is_object(_Base::as_json()); }
@@ -62,22 +67,29 @@ namespace jansson {
                inline const Value operator[](unsigned long index) const;
 
                // get value at array index (non-const version)
-               inline Value at(unsigned int index);
+               inline _ValueBase<_ArrayProxy> at(unsigned int index);
 
-               inline Value operator[](signed int index);
-               inline Value operator[](unsigned int index);
-               inline Value operator[](signed short index);
-               inline Value operator[](unsigned short index);
-               inline Value operator[](signed long index);
-               inline Value operator[](unsigned long index);
+               inline _ValueBase<_ArrayProxy> operator[](signed int index);
+               inline _ValueBase<_ArrayProxy> operator[](unsigned int index);
+               inline _ValueBase<_ArrayProxy> operator[](signed short index);
+               inline _ValueBase<_ArrayProxy> operator[](unsigned short index);
+               inline _ValueBase<_ArrayProxy> operator[](signed long index);
+               inline _ValueBase<_ArrayProxy> operator[](unsigned long index);
 
-               // get object property
+               // 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;
 
+               // get object property (non-const version)
+               inline _ValueBase<_ObjectProxy> get(const char* key);
+
+               inline _ValueBase<_ObjectProxy> get(const std::string& key);
+               inline _ValueBase<_ObjectProxy> operator[](const char* key);
+               inline _ValueBase<_ObjectProxy> operator[](const std::string& key);
+
                // clear all array/object values
                inline void clear();
 
@@ -149,6 +161,46 @@ namespace jansson {
                json_t* _value;
        };
 
+       // proxies an array element
+       class _ArrayProxy {
+       public:
+               // constructor
+               _ArrayProxy(json_t* array, unsigned int index) : _array(array), _index(index) {}
+
+               // assign to the proxied element
+               inline _ArrayProxy& operator=(const Value& value);
+
+               // get the proxied element
+               json_t* as_json() const { return json_array_get(_array, _index); }
+
+       private:
+               // array object we wrap
+               json_t* _array;
+
+               // index of property
+               unsigned int _index;
+       };
+
+       // proxies an object property
+       class _ObjectProxy {
+       public:
+               // constructor
+               _ObjectProxy(json_t* array, const char* key) : _object(array), _key(key) {}
+
+               // assign to the proxied element
+               inline _ObjectProxy& operator=(const Value& value);
+
+               // get the proxied element
+               json_t* as_json() const { return json_object_get(_object, _key); }
+
+       private:
+               // array object we wrap
+               json_t* _object;
+
+               // key of property
+               const char* _key;
+       };
+
        // represents any JSON value
        class Value : public _ValueBase<_Value> {
        public:
@@ -218,6 +270,11 @@ namespace jansson {
                        _iter = json_object_iter(_object.as_json());
                }
 
+               // construct a new iterator for a given object
+               Iterator(const _ValueBase<_ObjectProxy>& value) : _object(value.as_json()), _iter(0) {
+                       _iter = json_object_iter(_object.as_json());
+               }
+
                // increment iterator
                void next() {
                        _iter = json_object_iter_next(_object.as_json(), _iter);