mirror of
https://github.com/pocoproject/poco.git
synced 2025-11-25 15:19:21 +01:00
Merge pull request #457 from Kontinuation/develop
Update array elements when calling JSONConfiguration::set* with array[index] expression
This commit is contained in:
@@ -154,6 +154,9 @@ public:
|
|||||||
void add(const Dynamic::Var& value);
|
void add(const Dynamic::Var& value);
|
||||||
/// Add the given value to the array
|
/// Add the given value to the array
|
||||||
|
|
||||||
|
void 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;
|
void stringify(std::ostream& out, unsigned int indent = 0, int step = -1) const;
|
||||||
/// Prints the array to out. When indent has zero value,
|
/// Prints the array to out. When indent has zero value,
|
||||||
/// the array will be printed without newline breaks and spaces between elements.
|
/// the array will be printed without newline breaks and spaces between elements.
|
||||||
@@ -221,6 +224,13 @@ inline void Array::add(const Dynamic::Var& value)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline void Array::set(unsigned int index, const Dynamic::Var& value)
|
||||||
|
{
|
||||||
|
if (index >= _values.size()) _values.resize(index + 1);
|
||||||
|
_values[index] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
inline void Array::remove(unsigned int index)
|
inline void Array::remove(unsigned int index)
|
||||||
{
|
{
|
||||||
_values.erase(_values.begin() + index);
|
_values.erase(_values.begin() + index);
|
||||||
|
|||||||
@@ -991,6 +991,34 @@ void JSONTest::testDoubleElement()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void JSONTest::testSetArrayElement()
|
||||||
|
{
|
||||||
|
std::string json = "[]";
|
||||||
|
Parser parser;
|
||||||
|
Var result = parser.parse(json);
|
||||||
|
Poco::JSON::Array::Ptr array = result.extract<Poco::JSON::Array::Ptr>();
|
||||||
|
|
||||||
|
// array[0] = 7
|
||||||
|
array->set(0, 7);
|
||||||
|
assert(array->size() == 1);
|
||||||
|
assert(array->getElement<int>(0) == 7);
|
||||||
|
|
||||||
|
// array[2] = "foo"
|
||||||
|
array->set(2, std::string("foo"));
|
||||||
|
assert(array->size() == 3);
|
||||||
|
assert(array->getElement<int>(0) == 7);
|
||||||
|
assert(array->isNull(1));
|
||||||
|
assert(array->getElement<std::string>(2) == "foo");
|
||||||
|
|
||||||
|
// array[1] = 13
|
||||||
|
array->set(1, 13);
|
||||||
|
assert(array->size() == 3);
|
||||||
|
assert(array->getElement<int>(0) == 7);
|
||||||
|
assert(array->getElement<int>(1) == 13);
|
||||||
|
assert(array->getElement<std::string>(2) == "foo");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void JSONTest::testOptValue()
|
void JSONTest::testOptValue()
|
||||||
{
|
{
|
||||||
std::string json = "{ }";
|
std::string json = "{ }";
|
||||||
@@ -1717,6 +1745,7 @@ CppUnit::Test* JSONTest::suite()
|
|||||||
CppUnit_addTest(pSuite, JSONTest, testStringElement);
|
CppUnit_addTest(pSuite, JSONTest, testStringElement);
|
||||||
CppUnit_addTest(pSuite, JSONTest, testEmptyObjectElement);
|
CppUnit_addTest(pSuite, JSONTest, testEmptyObjectElement);
|
||||||
CppUnit_addTest(pSuite, JSONTest, testDoubleElement);
|
CppUnit_addTest(pSuite, JSONTest, testDoubleElement);
|
||||||
|
CppUnit_addTest(pSuite, JSONTest, testSetArrayElement);
|
||||||
CppUnit_addTest(pSuite, JSONTest, testOptValue);
|
CppUnit_addTest(pSuite, JSONTest, testOptValue);
|
||||||
CppUnit_addTest(pSuite, JSONTest, testQuery);
|
CppUnit_addTest(pSuite, JSONTest, testQuery);
|
||||||
CppUnit_addTest(pSuite, JSONTest, testComment);
|
CppUnit_addTest(pSuite, JSONTest, testComment);
|
||||||
|
|||||||
@@ -53,6 +53,7 @@ public:
|
|||||||
void testStringElement();
|
void testStringElement();
|
||||||
void testEmptyObjectElement();
|
void testEmptyObjectElement();
|
||||||
void testDoubleElement();
|
void testDoubleElement();
|
||||||
|
void testSetArrayElement();
|
||||||
void testOptValue();
|
void testOptValue();
|
||||||
void testQuery();
|
void testQuery();
|
||||||
void testComment();
|
void testComment();
|
||||||
|
|||||||
@@ -250,9 +250,7 @@ void JSONConfiguration::setValue(const std::string& key, const Poco::DynamicAny&
|
|||||||
KeyValue kv(key, sValue);
|
KeyValue kv(key, sValue);
|
||||||
|
|
||||||
if (eventsEnabled())
|
if (eventsEnabled())
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
||||||
propertyChanging(this, kv);
|
propertyChanging(this, kv);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -295,13 +293,11 @@ void JSONConfiguration::setValue(const std::string& key, const Poco::DynamicAny&
|
|||||||
}
|
}
|
||||||
arr = nextArray;
|
arr = nextArray;
|
||||||
}
|
}
|
||||||
arr->add(value);
|
arr->set(indexes.back(), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (eventsEnabled())
|
if (eventsEnabled())
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
||||||
propertyChanged(this, kv);
|
propertyChanged(this, kv);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -86,6 +86,36 @@ void JSONConfigurationTest::testLoad()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void JSONConfigurationTest::testSetArrayElement()
|
||||||
|
{
|
||||||
|
JSONConfiguration config;
|
||||||
|
|
||||||
|
std::string json = "{ \"config\" : "
|
||||||
|
" { \"prop1\" : \"value1\", "
|
||||||
|
" \"prop2\" : 10, "
|
||||||
|
" \"prop3\" : [ \"element1\", \"element2\" ], "
|
||||||
|
" \"prop4\" : { \"prop5\" : false, "
|
||||||
|
" \"prop6\" : null } "
|
||||||
|
" }"
|
||||||
|
"}";
|
||||||
|
|
||||||
|
std::istringstream iss(json);
|
||||||
|
config.load(iss);
|
||||||
|
|
||||||
|
// config.prop3[0] = "foo"
|
||||||
|
config.setString("config.prop3[0]", "foo");
|
||||||
|
assert(config.getString("config.prop3[0]") == "foo");
|
||||||
|
|
||||||
|
// config.prop3[1] = "bar"
|
||||||
|
config.setString("config.prop3[1]", "bar");
|
||||||
|
assert(config.getString("config.prop3[1]") == "bar");
|
||||||
|
|
||||||
|
// config.prop3[3] = "baz"
|
||||||
|
config.setString("config.prop3[3]", "baz");
|
||||||
|
assert(config.getString("config.prop3[3]") == "baz");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
AbstractConfiguration* JSONConfigurationTest::allocConfiguration() const
|
AbstractConfiguration* JSONConfigurationTest::allocConfiguration() const
|
||||||
{
|
{
|
||||||
return new JSONConfiguration;
|
return new JSONConfiguration;
|
||||||
@@ -108,6 +138,7 @@ CppUnit::Test* JSONConfigurationTest::suite()
|
|||||||
|
|
||||||
AbstractConfigurationTest_addTests(pSuite, JSONConfigurationTest);
|
AbstractConfigurationTest_addTests(pSuite, JSONConfigurationTest);
|
||||||
CppUnit_addTest(pSuite, JSONConfigurationTest, testLoad);
|
CppUnit_addTest(pSuite, JSONConfigurationTest, testLoad);
|
||||||
|
CppUnit_addTest(pSuite, JSONConfigurationTest, testSetArrayElement);
|
||||||
|
|
||||||
return pSuite;
|
return pSuite;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ public:
|
|||||||
virtual ~JSONConfigurationTest();
|
virtual ~JSONConfigurationTest();
|
||||||
|
|
||||||
void testLoad();
|
void testLoad();
|
||||||
|
void testSetArrayElement();
|
||||||
|
|
||||||
void setUp();
|
void setUp();
|
||||||
void tearDown();
|
void tearDown();
|
||||||
|
|||||||
Reference in New Issue
Block a user