fixed GH #99: JSON::Query an JSON::Object

- fixed GH #99: JSON::Query an JSON::Object
- swapped order of AnyCast(const Any&) and AnyCast(Any&) definitions
This commit is contained in:
Aleksandar Fabijanic
2013-06-07 23:15:02 -05:00
parent 1d2a95e110
commit 5e6ef1c14d
9 changed files with 447 additions and 48 deletions

View File

@@ -50,7 +50,6 @@ namespace JSON {
Query::Query(const Var& source): _source(source)
{
}
@@ -61,24 +60,56 @@ Query::~Query()
Object::Ptr Query::findObject(const std::string& path) const
{
Object::Ptr obj;
Var result = find(path);
if ( result.type() == typeid(Object::Ptr) )
{
obj = result.extract<Object::Ptr>();
}
if (result.type() == typeid(Object::Ptr))
return result.extract<Object::Ptr>();
else if (result.type() == typeid(Object))
return new Object(result.extract<Object>());
return 0;
}
Object& Query::findObject(const std::string& path, Object& obj) const
{
obj.clear();
Var result = find(path);
if (result.type() == typeid(Object::Ptr))
obj = *result.extract<Object::Ptr>();
else if (result.type() == typeid(Object))
obj = result.extract<Object>();
return obj;
}
Array::Ptr Query::findArray(const std::string& path) const
{
Array::Ptr arr;
Var result = find(path);
if ( result.type() == typeid(Array::Ptr) )
{
arr = result.extract<Array::Ptr>();
}
if (result.type() == typeid(Array::Ptr))
return result.extract<Array::Ptr>();
else if (result.type() == typeid(Array))
return new Array(result.extract<Array>());
return 0;
}
Array& Query::findArray(const std::string& path, Array& arr) const
{
arr.clear();
Var result = find(path);
if (result.type() == typeid(Array::Ptr))
arr = *result.extract<Array::Ptr>();
else if (result.type() == typeid(Array))
arr = result.extract<Array>();
return arr;
}
@@ -89,16 +120,16 @@ Var Query::find(const std::string& path) const
StringTokenizer tokenizer(path, ".");
for(StringTokenizer::Iterator token = tokenizer.begin(); token != tokenizer.end(); token++)
{
if ( !result.isEmpty() )
if (!result.isEmpty())
{
std::vector<int> indexes;
RegularExpression::MatchVec matches;
int firstOffset = -1;
int offset = 0;
RegularExpression regex("\\[([0-9]+)\\]");
while(regex.match(*token, offset, matches) > 0 )
while(regex.match(*token, offset, matches) > 0)
{
if ( firstOffset == -1 )
if (firstOffset == -1)
{
firstOffset = static_cast<int>(matches[0].offset);
}
@@ -108,33 +139,40 @@ Var Query::find(const std::string& path) const
}
std::string name(*token);
if ( firstOffset != -1 )
if (firstOffset != -1)
{
name = name.substr(0, firstOffset);
}
if ( name.length() > 0 )
if (name.length() > 0)
{
if ( result.type() == typeid(Object::Ptr) )
if (result.type() == typeid(Object::Ptr))
{
Object::Ptr o = result.extract<Object::Ptr>();
result = o->get(name);
}
else if (result.type() == typeid(Object))
{
Object o = result.extract<Object>();
result = o.get(name);
}
}
if (!result.isEmpty()
&& !indexes.empty() )
if (!result.isEmpty() && !indexes.empty())
{
for(std::vector<int>::iterator it = indexes.begin(); it != indexes.end(); ++it )
for(std::vector<int>::iterator it = indexes.begin(); it != indexes.end(); ++it)
{
if ( result.type() == typeid(Array::Ptr) )
if (result.type() == typeid(Array::Ptr))
{
Array::Ptr array = result.extract<Array::Ptr>();
result = array->get(*it);
if ( result.isEmpty() )
{
break;
}
if (result.isEmpty()) break;
}
else if (result.type() == typeid(Array))
{
Array array = result.extract<Array>();
result = array.get(*it);
if (result.isEmpty()) break;
}
}
}