Merge pull request #457 from Kontinuation/develop

Update array elements when calling JSONConfiguration::set* with array[index] expression
This commit is contained in:
Aleksandar Fabijanic 2014-05-23 10:37:38 -05:00
commit d45808f696
6 changed files with 73 additions and 5 deletions

View File

@ -154,6 +154,9 @@ public:
void add(const Dynamic::Var& value);
/// 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;
/// Prints the array to out. When indent has zero value,
/// 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)
{
_values.erase(_values.begin() + index);

View File

@ -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()
{
std::string json = "{ }";
@ -1717,6 +1745,7 @@ CppUnit::Test* JSONTest::suite()
CppUnit_addTest(pSuite, JSONTest, testStringElement);
CppUnit_addTest(pSuite, JSONTest, testEmptyObjectElement);
CppUnit_addTest(pSuite, JSONTest, testDoubleElement);
CppUnit_addTest(pSuite, JSONTest, testSetArrayElement);
CppUnit_addTest(pSuite, JSONTest, testOptValue);
CppUnit_addTest(pSuite, JSONTest, testQuery);
CppUnit_addTest(pSuite, JSONTest, testComment);

View File

@ -53,6 +53,7 @@ public:
void testStringElement();
void testEmptyObjectElement();
void testDoubleElement();
void testSetArrayElement();
void testOptValue();
void testQuery();
void testComment();

View File

@ -250,9 +250,7 @@ void JSONConfiguration::setValue(const std::string& key, const Poco::DynamicAny&
KeyValue kv(key, sValue);
if (eventsEnabled())
{
propertyChanging(this, kv);
}
@ -295,13 +293,11 @@ void JSONConfiguration::setValue(const std::string& key, const Poco::DynamicAny&
}
arr = nextArray;
}
arr->add(value);
arr->set(indexes.back(), value);
}
if (eventsEnabled())
{
propertyChanged(this, kv);
}
}

View File

@ -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
{
return new JSONConfiguration;
@ -108,6 +138,7 @@ CppUnit::Test* JSONConfigurationTest::suite()
AbstractConfigurationTest_addTests(pSuite, JSONConfigurationTest);
CppUnit_addTest(pSuite, JSONConfigurationTest, testLoad);
CppUnit_addTest(pSuite, JSONConfigurationTest, testSetArrayElement);
return pSuite;
}

View File

@ -27,6 +27,7 @@ public:
virtual ~JSONConfigurationTest();
void testLoad();
void testSetArrayElement();
void setUp();
void tearDown();