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

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

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

{
    "foo": "bar"
}

I know that we can check get_child_optional first. If the return value is null, we can check get_optional. But are there any better ways / apis for this?

+5
source share
1 answer

Try the following:

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;
+7
source

All Articles