0

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
}
Olumide
  • 5,397
  • 10
  • 55
  • 104
  • Found the answer `propertyTree.size()`. I've just undeleted this question because I thought the answer might be useful to someone else in the future. – Olumide Feb 05 '16 at 17:12
  • 1
    Then consider [writing your own answer](http://stackoverflow.com/help/self-answer) (you can also accept it after 48 hours). This will mark it as answered in search results. – rhashimoto Feb 05 '16 at 18:31
  • @Olumide what rhashimoto said – sehe Feb 05 '16 at 19:49

1 Answers1

0

Found the answer: propertyTree.size().

Olumide
  • 5,397
  • 10
  • 55
  • 104