1

Below are the steps I followed to fetch values from a JSON file:

{
  "Bases":[
    {
      "mnemonic":"ADIS.LA.01",
      "relay":true
    },
    {
      "mnemonic":"ALEX.LA.01",
      "relay":true
    }
  ]
}

I am failing to fetch the boolean values.

In the code below, I am:

  1. Opening the JSON file
  2. Setting the root element and start traversing the childtree under this root element (Bases)
  3. Fetching the values of each tag and saving them to appropriate variable types.

Code:

ReadJsonFile()
{
    using boost::property_tree::ptree;
    const boost::property_tree::ptree& propTree
    boost::property_tree::read_json(ss, pt);
    const std::string rootElement = "Bases"; 
    boost::property_tree::ptree childTree;
    bool m_relay;
    try
    {
        /** get_child - Get the child at the given path, or throw @c ptree_bad_path. */
        childTree = propTree.get_child(rootElement);
    }
    catch (boost::property_tree::ptree_bad_path& ex)
    {
        return false;
    }

    BOOST_FOREACH(const boost::property_tree::ptree::value_type &v, propTree.get_child(rootElement)){
       string vID;
       for (ptree::const_iterator subTreeIt = v.second.begin(); subTreeIt != v.second.end(); ++subTreeIt) {
          if (subTreeIt->first == "mnemonic")
          {
             // Get the value string and trim the extra spaces, if any
             vID = boost::algorithm::trim_copy( subTreeIt->second.data() );
          }
          if (subTreeIt->first == "relay")
          {
            m_relay = boost::algorithm::trim_copy(subTreeIt->second.data());
          }
       }
    }
 }

Error:

error: cannot convert ‘std::basic_string<char, std::char_traits<char>, std::allocator<char> >’ to ‘bool’ in assignment

Apparently the boolean value "relay":true is treated as a string instead of a bool.

If I change

bool m_relay;

to

std::string m_relay;

The code works fine, but the bool type is failing to compile.

Am I missing something?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
LearningCpp
  • 972
  • 12
  • 29
  • Might want to look at https://stackoverflow.com/questions/4597048/boost-property-tree-iterators-how-to-handle-them – Component 10 May 24 '17 at 13:25
  • I looked into that , it does'nt provide info about fetching boolen value using ptree::const_iterator and thus i asked the question – LearningCpp May 24 '17 at 13:33
  • @LearningCpp: actually, it does provide details of how to fetch different data types, if you read it more carefully. Try using `m_relay = subTreeIt->second.get_value();` instead of `m_relay = boost::algorithm::trim_copy(subTreeIt->second.data());` – Remy Lebeau May 24 '17 at 17:57

2 Answers2

1

Try using this:

m_relay = subTreeIt->second.get_value<bool>();

Instead of this:

m_relay = boost::algorithm::trim_copy(subTreeIt->second.data());
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

You have to cast it manually:

boost::lexical_cast<bool>(subTreeIt->second.data());

But it doesn't seem like a preferred way. I urge you to read Docs: How to Access Data in a Property Tree.

However, I don't see another way using an iterator. So I guess you are fine, given that you already have it. The preferred ways seem to hinge on the path, which you do not use.

For what it's worth... You probably should use find instead of iterating manually, or refactor your code for some version of get. It seems like a better design.

luk32
  • 15,812
  • 38
  • 62