Search code examples
c++jsonboostboost-propertytree

Testing if boost property tree stores a primitive or a subtree


I'm parsing a JSON file in which the value corresponding to a key can be a primitive (string) or a subtree. Useful for example for storing information about people with a single employer, for example

{
    "employer" : "NASA";
}

or people with multiple employers, for example

{
    "employer" :
    {
        "weekdays" : "Taco Bell" ,
        "weekends" : "Google Inc"
    }
}

While parsing the employer key I need to test whether its property tree value stores a primitive (single employer) or a subtree (multiple employers). I've tried get_value_optional as shown below, but I still get an initialized employerName that's an empty string. Is there another way to go about this.

boost::optional<std::string> employerName = propertyTree.get_value_optional<std::string>();
if( employerName.is_initialized() )
{
    std::string name = employerName.get(); // returns empty string if propertyTree stores a subtree
}

Solution

  • Found the answer: propertyTree.size().