Parsing object inside array in rapidjson

只谈情不闲聊 提交于 2019-12-24 03:51:10

问题


I'm having problems implementing a recursive function that goes over the tree I get from the parsing of a json input.

json input. e.g.:

{
  "attr" : { "a": 1, "ovec": [ { "b": 2, "c": 3 }, { "d": 4} ] }
}

This is what we call a 'compound value of an attribute', and the value is simply a JSON doc. Its content is completely arbitrary (as long as its valid JSON).

The problem is that with a Vector I have to loop using the type Value::ConstValueIterator (unlike for Object, where I use Value::ConstMemberIterator).

My recursive function has Value::ConstMemberIterator as parameter and all is OK until I encounter a Vector/Object inside a Vector - for the recursive call I'd need an iterator of the type Value::ConstMemberIterator.

Relevant parts of the "traversing" function:

int parseContextAttributeCompoundValue
(
    const Value::ConstMemberIterator&  node
)
{
    std::string type = jsonParseTypeNames[node->value.GetType()];
    if (type == "Array")
    {
        for (Value::ConstValueIterator iter = node->value.Begin(); iter != node->value.End(); ++iter)
        {
            std::string nodeType = jsonParseTypeNames[iter->value.GetType()];
            if (nodeType == "String")
            {
                val = iter->GetString();
            }
            // else if ...
            if ((nodeType == "Object") || (nodeType == "Array"))
            {
                // Here's my problem - need to convert 'iter' to Value::ConstMemberIterator
                // in order to recursively call parseContextAttributeCompoundValue for this object/array
                parseContextAttributeCompoundValue(iter); // COMPILATION ERROR
            }
        }
    }
    else if (type == "Object")
    {
        for (Value::ConstMemberIterator iter = node->value.MemberBegin(); iter != node->value.MemberEnd(); ++iter)
        {
            std::string nodeType = jsonParseTypeNames[iter->value.GetType()];
            if (nodeType == "String")
            {
                val = iter->value.GetString();
            }
            else if (nodeType == "Number")
            {

            if ((nodeType == "Object") || (nodeType == "Array"))
            {
                // Here I'm just fine as iter is of the desired type already
                parseContextAttributeCompoundValue(iter);
            }
        }
    }
}

I've tried a few things like calling iter->value.MemberBegin() to "convert" to the desired type, but so far without any success

More than thankful for some help here ...


回答1:


You can simply call a function with a Value type, instead of passing iterator:

void parseContextAttributeCompoundValue(const Value& v) {
    if (v.IsObject()) {
        // ...
    }
    else if (v.IsArray() {
        // ...
    }
}

And then from the calling site:

for (Value::ConstValueIterator iter = ...) {
    parseContextAttributeCompoundValue(*iter);
}

for (Value::ConstMemberIterator iter = ...) {
    parseContextAttributeCompoundValue(iter->value);
}


来源:https://stackoverflow.com/questions/31383852/parsing-object-inside-array-in-rapidjson

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!