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?
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With