If a user happens to store an ElementProxy or a PropertyProxy
instance, we need to take a reference to the JSON value they point to.
With PropertyProxy, the key needs to be copied as well.
// proxies an array element
class ElementProxy {
public:
// proxies an array element
class ElementProxy {
public:
- // constructor
- ElementProxy(json_t* array, unsigned int index) : _array(array), _index(index) {}
+ ElementProxy(json_t* array, unsigned int index);
+ ElementProxy(const ElementProxy& other);
+ ~ElementProxy();
// assign to the proxied element
inline ElementProxy& operator=(const Value& value);
// assign to the proxied element
inline ElementProxy& operator=(const Value& value);
// proxies an object property
class PropertyProxy {
public:
// proxies an object property
class PropertyProxy {
public:
- // constructor
- PropertyProxy(json_t* array, const char* key) : _object(array), _key(key) {}
+ PropertyProxy(json_t* object, const char *key);
+ PropertyProxy(const PropertyProxy& other);
+ ~PropertyProxy();
// assign to the proxied element
inline PropertyProxy& operator=(const Value& value);
// assign to the proxied element
inline PropertyProxy& operator=(const Value& value);
json_t* _object;
// key of property
json_t* _object;
// key of property
};
} // namespace json::detail
};
} // namespace json::detail
#error "jansson.ipp may only be included from jansson.hpp"
#endif
#error "jansson.ipp may only be included from jansson.hpp"
#endif
namespace json {
namespace detail {
// assignment operator
namespace json {
namespace detail {
// assignment operator
+ 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());
// assign value to proxied array element
ElementProxy& ElementProxy::operator=(const Value& value) {
json_array_set(_array, _index, value.as_json());
return json_array_get(_array, _index);
}
return json_array_get(_array, _index);
}
+ PropertyProxy::PropertyProxy(json_t* object, const char* key)
+ : _object(object) {
+ _key = strdup(key);
+ json_incref(_object);
+ }
+
+ PropertyProxy::PropertyProxy(const PropertyProxy& other)
+ : _object(other._object) {
+ _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());
// assign value to proxied object property
PropertyProxy& PropertyProxy::operator=(const Value& value) {
json_object_set(_object, _key, value.as_json());