class Iterator;
class Value;
+ class _ArrayProxy;
// base class for JSON value interface
template <typename _Base>
// 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()); }
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
inline const Value get(const char* key) const;
json_t* _value;
};
+ // proxies an array element
+ class _ArrayProxy {
+ public:
+ // construct new Value with an undefined value
+ _ArrayProxy() : _array(0), _index(0) {}
+
+ // 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;
+ };
+
// represents any JSON value
class Value : public _ValueBase<_Value> {
public:
// get value at array index (non-const version)
template <typename _Base>
-jansson::Value jansson::_ValueBase<_Base>::at(unsigned int index) {
- return jansson::Value(json_array_get(_Base::as_json(), index));
+jansson::_ValueBase<jansson::_ArrayProxy> jansson::_ValueBase<_Base>::at(unsigned int index) {
+ return _ArrayProxy(_Base::as_json(), index);
}
template <typename _Base>
-jansson::Value jansson::_ValueBase<_Base>::operator[](signed int index) { return at(index); }
+jansson::_ValueBase<jansson::_ArrayProxy> jansson::_ValueBase<_Base>::operator[](signed int index) { return at(index); }
template <typename _Base>
-jansson::Value jansson::_ValueBase<_Base>::operator[](unsigned int index) { return at(index); }
+jansson::_ValueBase<jansson::_ArrayProxy> jansson::_ValueBase<_Base>::operator[](unsigned int index) { return at(index); }
template <typename _Base>
-jansson::Value jansson::_ValueBase<_Base>::operator[](signed short index) { return at(index); }
+jansson::_ValueBase<jansson::_ArrayProxy> jansson::_ValueBase<_Base>::operator[](signed short index) { return at(index); }
template <typename _Base>
-jansson::Value jansson::_ValueBase<_Base>::operator[](unsigned short index) { return at(index); }
+jansson::_ValueBase<jansson::_ArrayProxy> jansson::_ValueBase<_Base>::operator[](unsigned short index) { return at(index); }
template <typename _Base>
-jansson::Value jansson::_ValueBase<_Base>::operator[](signed long index) { return at(index); }
+jansson::_ValueBase<jansson::_ArrayProxy> jansson::_ValueBase<_Base>::operator[](signed long index) { return at(index); }
template <typename _Base>
-jansson::Value jansson::_ValueBase<_Base>::operator[](unsigned long index) { return at(index); }
+jansson::_ValueBase<jansson::_ArrayProxy> jansson::_ValueBase<_Base>::operator[](unsigned long index) { return at(index); }
// get object property
template <typename _Base>
json_array_insert(_Base::as_json(), index, value._Base::as_json());
return *this;
}
+
+// assign value to proxied array element
+jansson::_ArrayProxy& jansson::_ArrayProxy::operator=(const Value& value) {
+ json_array_set(_array, _index, value.as_json());
+ return *this;
+}
e14.set_key("foo", jansson::Value::object());
ASSERT_TRUE(e14["foo"].is_object(), "e14.foo is not an object after assignment");
//e14["foo"]["bar"] = jansson::Value::from(42);
- //ASSERT_EQ(e14["foo"]["bar"].as_integer(), 42, "e14.foo.bar has incorrecy value after assignment");
-
+ //ASSERT_EQ(e14["foo"]["bar"].as_integer(), 42, "e14.foo.bar has incorrect value after assignment");
+
+ jansson::Value e15(jansson::Value::array());
+ ASSERT_TRUE(e15.is_array(), "e15 is not an array after construction");
+ e15.set_at(0, jansson::Value::from(42));
+ ASSERT_EQ(e15[0].as_integer(), 42, "e15[0] has incorrect value after assignment");
+ e15[0] = jansson::Value::from("foo");
+ ASSERT_EQ(e15[0].as_string(), "foo", "e15[0] has incorrecy value after assignment");
return 0;
}