4

I'm using boost property trees to read values from a json file.

{
    "some_values":
    {
        "field_1":  "value_1",
        "field_2":  true
    }
}

I can read the values with:

spTree->get<string>("some_values.field_1",  "");
spTree->get<bool>("some_values.field_2",    false);

But can I read the type of the variable stored in any given field?

stack user
  • 835
  • 1
  • 9
  • 28
  • ptree is defined as `typedef basic_ptree< std::string, std::string > ptree;`, and doesn't encode any type information. All properties are stored as string, so I don't think it's possible to get type information at all. – shash Jan 30 '17 at 09:09

1 Answers1

5

Documentation says

[...] the following JSON / property tree mapping is used:

[...] JSON values are mapped to nodes containing the value. However, all type information is lost; numbers, as well as the literals "null", "true" and "false" are simply mapped to their string form. Property tree nodes containing both child nodes and data cannot be mapped.

so there is no way if you intend to use the JSON parser unless you write your own code or add additional metadata.

Marco A.
  • 43,032
  • 26
  • 132
  • 246