boost::write_json



  • Hey, I constructed a property tree that I write into a file using write_xml. Now I would like to write the same propertytree to a json file, but it doesn't work.

    I thought I could just use my generated property tree without any alteration.

    I think I didnt really understand how the json format works.
    when I write something in json all the type information gets lost, right? and also i cannot have any unnamed nodes, right?

    I actually don't get anything working with json.
    this is an example from the internet:
    Its all fine with xml, but it doesnt work with json. Can anybody help me? Maybe it would already help if I had an example that actually compiles...

    #include <boost/property_tree/json_parser.hpp>
    #include <boost/property_tree/xml_parser.hpp>
    #include <boost/array.hpp>
    
    boost::property_tree::ptree foo()
    {
      boost::property_tree::ptree retval;
      retval.put("moo","12345");
      //retval.put("zoo",12345);
      return retval;
    }
    
    boost::property_tree::ptree bar()
    {
      boost::property_tree::ptree retval;
      retval.put("mork","hello");
      return retval;
    }
    
    int main()
    {
      boost::property_tree::ptree pt;
      pt.put_child("foo", foo() );
      pt.put_child("bar", bar() );
      boost::property_tree::write_json("file.json", pt );
      //boost::property_tree::write_xml("file.xml", pt );
      }
    


  • Annah.Do schrieb:

    Its all fine with xml, but it doesnt work with json. Can anybody help me? Maybe it would already help if I had an example that actually compiles...

    Compiles and runs fine with Boost 1.52 on Linux.
    Outputs:

    {
        "foo":
        {
            "moo": "12345"
        },
        "bar":
        {
            "mork": "hello"
        }
    }
    


  • Hm. Ok So I guess my version of Boost is a bit too old.
    Thank you.


Anmelden zum Antworten