title: How to convert any value to an object and add members with boost::property_tree json
body: <p>I have a program that modifies a JSON document if necessary. The program has to add a child to another value whether or not it's an already an object. The program should behave like so:</p>
  
  <ol>
  <li>If the object with key "x" does not exist, create object with key "x" and add value y as a child.</li>
  <li>If the object with key "x" DOES exist, set value y as a child.</li>
  <li>If the key "x" exists and is ANY OTHER type, delete it, create an object with the key "x" and then add value y as a child.</li>
  </ol>
  
  <p>I see ways to test if property tree values exist or whether they are specified types, but none to test if it's an object or not an object.</p>
  
  <p>Here's a simple program I made illustrating what I mean:</p>
  
  <pre><code>#include &lt;boost/property_tree/ptree.hpp&gt;
  #include &lt;boost/property_tree/json_parser.hpp&gt;
  
  #include &lt;sstream&gt;
  #include &lt;iostream&gt;
  
  const char *json = "{"
  "\"object\" : { \"mighty\" : \"wind\" },"
  "\"boolean\" : true"
  "}";
  
  void printTree( std::string name, boost::property_tree::ptree tree )
  {
      std::cout &lt;&lt; "Pass '" &lt;&lt; name &lt;&lt; "'" &lt;&lt; std::endl;
      try
      {
          std::stringstream ss;
          boost::property_tree::write_json( ss, tree );
          std::cout &lt;&lt; ss.str() &lt;&lt; std::endl;
      }
      catch( std::exception &amp;e )
      {
          std::cout &lt;&lt; "Could not make create json: " &lt;&lt; e.what() &lt;&lt; std::endl;
      }
  }
  
  int main( int argc, char *argv[] )
  {
      boost::property_tree::ptree tree;
  
      // Load it
      std::istringstream ss_json( json );
      boost::property_tree::read_json( ss_json, tree );
  
      // Add a value to an object that doesn't exist
      tree.put( "none.value", "hello!" );
  
      // Print to see
      printTree( "Nonexistent value test", tree );
  
      // Add a value to the object
      tree.put( "object.value", "bello!" );
  
      // Print this one
      printTree( "Adding value test", tree );
  
      // Convert boolean to an object and add a value
      tree.put( "boolean.value", "mello!" );
  
      // Print it
      printTree( "Converting value test", tree );
  }
  </code></pre>
  
  <p>The output will be:</p>
  
  <pre><code>Pass 'Nonexistent value test'
  {
      "object": {
          "mighty": "wind"
      },
      "boolean": "true",
      "none": {
          "value": "hello!"
      }
  }
  
  Pass 'Adding value test'
  {
      "object": {
          "mighty": "wind",
          "value": "bello!"
      },
      "boolean": "true",
      "none": {
          "value": "hello!"
      }
  }
  </code></pre>
  
  <blockquote>
    <p>Pass 'Converting value test'<br>
    Could not make create json: &lt;unspecified file>: ptree contains data that cannot be represented in JSON format</p>
  </blockquote>
  
  <p>You can see in the output, the last step fails to convert to JSON (doesn't throw when I try to set it).</p>
  
  <p>How can I achieve scenario 3 in my list above?</p>