Boost logo

Boost Users :

Subject: [Boost-users] Boost PropertyTree: read_json() followed by write_xml() produces invalid XML
From: Marcel Loose (loose_at_[hidden])
Date: 2014-05-05 17:38:35


Hi all,

Last week I posted the message below to the Boost Users group. One of the posters suggested that I stumbled upon a bug in Boost.PropertyTree.
So, the question is: am I doing something wrong, or is this is a bug in Boost.PropertyTree?

Best regards,
Marcel Loose.

---- Start of message ----

I was quite surprised and disappointed to find out that I was unable
to convert a simple JSON file into XML, using the Boost PropertyTree.
As input I used the example JSON file from the documentation (see
example.json below).

I wrote a small program (see example.cc below) that reads in this JSON
file, writes it to disk as XML file, and finally tries to read back
this XML file. This last operation fails with an exception (see output
below); the XML file that is produced (see example.xml below) is
invalid, because it contains empty tags (<>).

Am I doing something wrong?

Best regards,
Marcel Loose.

$ cat example.json
{
   "menu":
   {
      "foo": true,
      "bar": "true",
      "value": 102.3E+06,
      "popup":
      [
         {"value": "New", "onclick": "CreateNewDoc()"},
         {"value": "Open", "onclick": "OpenDoc()"}
      ]
   }
}

$ cat example.cc
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <fstream>

using namespace std;
using namespace boost::property_tree;

int main()
{
  ptree pt;
  {
    read_json("example.json", pt);
    ofstream ofs("example.xml");
    write_xml(ofs, pt, xml_writer_make_settings(' ', 2));
  }
  {
    ifstream ifs("example.xml");
    read_xml(ifs, pt);
  }
  return 0;
}

$ ./example
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::property_tree::xml_parser::xml_parser_error> >'
  what(): <unspecified file>(7): expected element name
Aborted

$ cat example.xml
<?xml version="1.0" encoding="utf-8"?>
<menu>
  <foo>true</foo>
  <bar>true</bar>
  <value>102.3E+06</value>
  <popup>
    <>
      <value>New</value>
      <onclick>CreateNewDoc()</onclick>
    </>
    <>
      <value>Open</value>
      <onclick>OpenDoc()</onclick>
    </>
  </popup>
</menu>

---- End of message ----


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net