1

I want to print the json array using below code snippet but it is not giving the desired result . Here is the output

{
    "prefix": "standard",
    "faceID": "42"
}

{
    "prefix1": "standard2",
    "faceID2": "44"
}

This is produced by below code snippet:

#include <boost/serialization/string.hpp>
#include <string>
#include <sstream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
using boost::property_tree::ptree; 
using boost::property_tree::write_json;

void create_array(ptree parent)
{
    std::stringstream oss;

    write_json(oss,parent);

    std::string serialized_strings(oss.str());

    std::cout << oss.str() << std::endl;
}

int main()                                                                                                                                                                                                 
{   

    ptree pt,pt1;

    pt.put("prefix","standard");
    pt.put("faceID",42);
    create_array(pt);
    pt1.put("prefix1","standard2");
    pt1.put("faceID2",44);
    create_array(pt1); 

}

expected output:

[
{
    "prefix": "standard",
    "faceID": "42"
},

{
    "prefix1": "standard2",
    "faceID2": "44"
}
]
sehe
  • 374,641
  • 47
  • 450
  • 633
user2997518
  • 812
  • 6
  • 17
  • Please, searching can't be that hard, even if reading the ~20 lines of documentation is too much. – sehe Sep 08 '17 at 20:26
  • Dear @sehe :I already went through that link and It looks like in boost there is no solution for this problem and i don't want to create the name of array for binding the elements so looking for alternate solution . – user2997518 Sep 09 '17 at 00:05
  • You do not "want" to what now? "Create the name"? Did you read about [the limitations in those lines of documentation](http://www.boost.org/doc/libs/1_65_1/doc/html/property_tree/parsers.html#property_tree.parsers.json_parser)? (link fixed) – sehe Sep 09 '17 at 00:07
  • solution given in below link doesn't fulfill my requiremnt.https://stackoverflow.com/questions/2114466/creating-json-arrays-in-boost-using-property-trees – user2997518 Sep 09 '17 at 00:08

1 Answers1

1

Just to make it absolutely clear:

The documentation states that

The property tree dataset is not typed, and does not support arrays as such. Thus, the following JSON / property tree mapping is used [...]

It continues to describe that each ptree represents a JSON object, always.

You need to remember that Boost Property Tree is not a JSON library. It is a Property Tree library, that optionally uses a subset of JSON for interoperability purposes. Therefore you cannot have arbitrary JSON things: You can not have a top-level arrays, you cannot have lone values, you cannot have actual numeric types, null, booleans etc.

You can only have Property Trees serialized using the described mappings.

sehe
  • 374,641
  • 47
  • 450
  • 633
  • I completely agree with you. i can understand that we can't have arbitrary json things with boost and that's why i had pointed out in my question that how to address this issue . should we go for other library to achieve this ? – user2997518 Sep 09 '17 at 00:24
  • Yes. (By the way, it was _not_ clear to me that you wanted the array at top-level, neither was it clear that you are solliciting advice for alternative tools. That question, by the way, would be off-topic for [SO]. I personally recommend http://rapidjson.org/md_doc_tutorial.html or https://github.com/nlohmann/json) – sehe Sep 09 '17 at 00:25
  • accepted this with +1 but not sure why my question is marked for downvote. if other than boost still solution is available in other library then suggestion should come. – user2997518 Sep 09 '17 at 00:33