fixed GH #2249: Poco::JSON::Object::set() should return reference to this to allow chaining

This commit is contained in:
Günter Obiltschnig 2018-03-27 11:47:05 +02:00
parent 35ba02e4a9
commit 5a1bf5eb4a
3 changed files with 20 additions and 17 deletions

View File

@ -34,10 +34,10 @@ class Object;
class JSON_API Array
/// Represents a JSON array. Array provides a representation
/// based on shared pointers and optimized for performance. It is possible to
/// based on shared pointers and optimized for performance. It is possible to
/// convert Array to Poco::Dynamic::Array. Conversion requires copying and therefore
/// has performance penalty; the benefit is in improved syntax, eg:
///
///
/// // use pointers to avoid copying
/// using namespace Poco::JSON;
/// std::string json = "[ {\"test\" : 0}, { \"test1\" : [1, 2, 3], \"test2\" : 4 } ]";
@ -49,7 +49,7 @@ class JSON_API Array
/// Object::Ptr subObject = *arr->getObject(1); // subObject == {\"test\" : 0}
/// Array subArr::Ptr = subObject->getArray("test1"); // subArr == [1, 2, 3]
/// i = result = subArr->get(0); // i == 1;
///
///
/// // copy/convert to Poco::Dynamic::Array
/// Poco::Dynamic::Array da = *arr;
/// i = da[0]["test"]; // i == 0
@ -102,7 +102,7 @@ public:
/// Returns the end iterator for values.
Dynamic::Var get(unsigned int index) const;
/// Retrieves the element at the given index.
/// Retrieves the element at the given index.
/// Will return an empty value when the element doesn't exist.
Array::Ptr getArray(unsigned int index) const;
@ -172,10 +172,10 @@ public:
return value;
}
void add(const Dynamic::Var& value);
Array& add(const Dynamic::Var& value);
/// Add the given value to the array
void set(unsigned int index, const Dynamic::Var& value);
Array& set(unsigned int index, const Dynamic::Var& value);
/// Update the element on the given index to specified value
void stringify(std::ostream& out, unsigned int indent = 0, int step = -1) const;
@ -264,18 +264,20 @@ inline bool Array::isArray(ConstIterator& it) const
}
inline void Array::add(const Dynamic::Var& value)
inline Array& Array::add(const Dynamic::Var& value)
{
_values.push_back(value);
_modified = true;
return *this;
}
inline void Array::set(unsigned int index, const Dynamic::Var& value)
inline Array& Array::set(unsigned int index, const Dynamic::Var& value)
{
if (index >= _values.size()) _values.resize(index + 1);
_values[index] = value;
_modified = true;
return *this;
}

View File

@ -42,18 +42,18 @@ class JSON_API Object
/// shared pointers and optimized for performance. It is possible to
/// convert Object to DynamicStruct. Conversion requires copying and therefore
/// has performance penalty; the benefit is in improved syntax, eg:
///
///
/// std::string json = "{ \"test\" : { \"property\" : \"value\" } }";
/// Parser parser;
/// Var result = parser.parse(json);
///
///
/// // use pointers to avoid copying
/// Object::Ptr object = result.extract<Object::Ptr>();
/// Var test = object->get("test"); // holds { "property" : "value" }
/// Object::Ptr subObject = test.extract<Object::Ptr>();
/// test = subObject->get("property");
/// std::string val = test.toString(); // val holds "value"
///
///
/// // copy/convert to Poco::DynamicStruct
/// Poco::DynamicStruct ds = *object;
/// val = ds["test"]["property"]; // val holds "value"
@ -148,7 +148,7 @@ public:
Poco::Nullable<T> getNullableValue(const std::string& key) const
/// Retrieves the property with the given name and will
/// try to convert the value to the given template type.
///
///
/// The convert<T> method of Var is called
/// which can also throw exceptions for invalid values.
/// Note: This will not work for an array or an object.
@ -209,13 +209,13 @@ public:
std::size_t size() const;
/// Returns the number of properties.
void set(const std::string& key, const Dynamic::Var& value);
Object& set(const std::string& key, const Dynamic::Var& value);
/// Sets a new value.
void stringify(std::ostream& out, unsigned int indent = 0, int step = -1) const;
/// Prints the object to out stream.
/// Prints the object to out stream.
///
/// When indent is 0, the object will be printed on a single
/// When indent is 0, the object will be printed on a single
/// line without indentation.
void remove(const std::string& key);

View File

@ -199,7 +199,7 @@ const std::string& Object::getKey(KeyList::const_iterator& iter) const
}
void Object::set(const std::string& key, const Dynamic::Var& value)
Object& Object::set(const std::string& key, const Dynamic::Var& value)
{
std::pair<ValueMap::iterator, bool> ret = _values.insert(ValueMap::value_type(key, value));
if (!ret.second) ret.first->second = value;
@ -209,11 +209,12 @@ void Object::set(const std::string& key, const Dynamic::Var& value)
KeyList::iterator end = _keys.end();
for (; it != end; ++it)
{
if (key == (*it)->first) return;
if (key == (*it)->first) return *this;
}
_keys.push_back(ret.first);
}
_modified = true;
return *this;
}