Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if a value in a boost property tree is a tree or a terminal value

I have been looking for APIs in boost::property_tree (that is used for reading a json) that I can use to determine if a value of a field is a tree or a terminal value. For example, I have a json where the value of foo can either be a tree as illustrated in the first block or a string as illustrated in the second block.

{
    "foo": {
        " n1": "v1",
        "n2": "v2"
    }
}

{
    "foo": "bar"
}

I know we can check first with get_child_optional. If the returned value is null, then we can check get_optional. But are there any better ways/apis to do this?

like image 548
rama44ster Avatar asked Mar 04 '13 05:03

rama44ster


1 Answers

Try this:

property_tree pt;
...

if(pt.empty())
    cout << "Node doesn't have children" << endl;

if(pt.data.empty())
    cout << "Node doesn't have data" << endl;

if(pt.empty() && !pt.data.empty())
    cout << "Node is terminal value" << endl;

if(!pt.empty() && pt.data.empty())
    cout << "Node is a tree" << endl;
like image 126
AlexT Avatar answered Nov 18 '22 03:11

AlexT