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"
}
]
4
  • Please, searching can't be that hard, even if reading the ~20 lines of documentation is too much. Commented Sep 8, 2017 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 . Commented Sep 9, 2017 at 0:05
  • You do not "want" to what now? "Create the name"? Did you read about the limitations in those lines of documentation? (link fixed) Commented Sep 9, 2017 at 0:07
  • solution given in below link doesn't fulfill my requiremnt.stackoverflow.com/questions/2114466/… Commented Sep 9, 2017 at 0:08

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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 ?
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 Stack Overflow. I personally recommend rapidjson.org/md_doc_tutorial.html or github.com/nlohmann/json)
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.