Var/JSON changes

- JSON support for Dynamic Struct and Array
- added VarIterator
- modified behavior of empty Var
This commit is contained in:
Aleksandar Fabijanic
2013-05-27 21:00:14 -05:00
parent 1f8fcaacc0
commit 38131d452d
42 changed files with 1878 additions and 308 deletions

View File

@@ -116,10 +116,22 @@ bool Array::isNull(unsigned int index) const
bool Array::isObject(unsigned int index) const
{
Var value = get(index);
return isObject(value);
}
bool Array::isObject(const Dynamic::Var& value) const
{
return value.type() == typeid(Object::Ptr);
}
bool Array::isObject(ConstIterator& it) const
{
return it!= end() && isObject(*it);
}
void Array::stringify(std::ostream& out, unsigned int indent, int step) const
{
if (step == -1) step = indent;
@@ -152,4 +164,62 @@ void Array::stringify(std::ostream& out, unsigned int indent, int step) const
}
Array::operator const Poco::Dynamic::Array& () const
{
if (!_pArray)
{
ValueVec::const_iterator it = _values.begin();
ValueVec::const_iterator end = _values.end();
_pArray = new Poco::Dynamic::Array;
int index = 0;
for (; it != end; ++it, ++index)
{
if (isObject(it))
{
_pArray->insert(_pArray->end(), Poco::JSON::Object::makeStruct(getObject(index)));
}
else if (isArray(it))
{
_pArray->insert(_pArray->end(), makeArray(getArray(index)));
}
else
{
_pArray->insert(_pArray->end(), *it);
}
}
}
return *_pArray;
}
Poco::Dynamic::Array Array::makeArray(const JSON::Array::Ptr& arr)
{
Poco::Dynamic::Array vec;
JSON::Array::ConstIterator it = arr->begin();
JSON::Array::ConstIterator end = arr->end();
int index = 0;
for (; it != end; ++it, ++index)
{
if (arr->isObject(it))
{
Object::Ptr pObj = arr->getObject(index);
DynamicStruct str = Poco::JSON::Object::makeStruct(pObj);
vec.insert(vec.end(), str);
}
else if (arr->isArray(it))
{
Array::Ptr pArr = arr->getArray(index);
std::vector<Poco::Dynamic::Var> v = makeArray(pArr);
vec.insert(vec.end(), v);
}
else
vec.insert(vec.end(), *it);
}
return vec;
}
} } // namespace Poco::JSON