class Iterator;
class Value;
class _ArrayProxy;
+ class _ObjectProxy;
// base class for JSON value interface
template <typename _Base>
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();
// 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) {}
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:
_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);
template <typename _Base>
jansson::_ValueBase<jansson::_ArrayProxy> jansson::_ValueBase<_Base>::operator[](unsigned long index) { return at(index); }
-// get object property
+// get object property (const version)
template <typename _Base>
const jansson::Value jansson::_ValueBase<_Base>::get(const char* key) const {
return jansson::Value(json_object_get(_Base::as_json(), key));
template <typename _Base>
const jansson::Value jansson::_ValueBase<_Base>::operator[](const std::string& key) const { return get(key.c_str()); }
+// get object property (non-const version)
+template <typename _Base>
+jansson::_ValueBase<jansson::_ObjectProxy> jansson::_ValueBase<_Base>::get(const char* key) {
+ return _ObjectProxy(_Base::as_json(), key);
+}
+
+template <typename _Base>
+jansson::_ValueBase<jansson::_ObjectProxy> jansson::_ValueBase<_Base>::get(const std::string& key) { return get(key.c_str()); }
+template <typename _Base>
+jansson::_ValueBase<jansson::_ObjectProxy> jansson::_ValueBase<_Base>::operator[](const char* key) { return get(key); }
+template <typename _Base>
+jansson::_ValueBase<jansson::_ObjectProxy> jansson::_ValueBase<_Base>::operator[](const std::string& key) { return get(key.c_str()); }
+
// clear all array/object values
template <typename _Base>
void jansson::_ValueBase<_Base>::clear() {
json_array_set(_array, _index, value.as_json());
return *this;
}
+
+// assign value to proxied object property
+jansson::_ObjectProxy& jansson::_ObjectProxy::operator=(const Value& value) {
+ json_object_set(_object, _key, value.as_json());
+ return *this;
+}
ASSERT_TRUE(e14.is_object(), "e14 is not an object after construction");
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 incorrect value after assignment");
+ e14["foo"]["bar"] = jansson::Value::from(42);
+ 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");