Zdeno Pavlik
Zdeno Pavlik

Reputation: 758

Create array of json using boost property_tree

I am having troubles with creating array of json using boost library, property tree in C++.

I took as a reference this thread, especially this part

ptree pt;
ptree children;
ptree child1, child2, child3;


child1.put("childkeyA", 1);
child1.put("childkeyB", 2);

child2.put("childkeyA", 3);
child2.put("childkeyB", 4);

child3.put("childkeyA", 5);
child3.put("childkeyB", 6);

children.push_back(std::make_pair("", child1));
children.push_back(std::make_pair("", child2));
children.push_back(std::make_pair("", child3));

pt.put("testkey", "testvalue");
pt.add_child("MyArray", children);

write_json("test2.json", pt);

results in:

{
    "testkey": "testvalue",
    "MyArray":
    [
        {
            "childkeyA": "1",
            "childkeyB": "2"
        },
        {
            "childkeyA": "3",
            "childkeyB": "4"
        },
        {
            "childkeyA": "5",
            "childkeyB": "6"
        }
    ]
}

But what can I do if I want to achieve just simple array without any object containing it? Like this:

[
    {
        "childkeyA": "1",
        "childkeyB": "2"
    },
    {
        "childkeyA": "3",
        "childkeyB": "4"
    },
    {
        "childkeyA": "5",
        "childkeyB": "6"
    }
]

Thank you a lot.

Upvotes: 4

Views: 2061

Answers (2)

Zdeno Pavlik
Zdeno Pavlik

Reputation: 758

Finally I haven't found solution using boost library. But this can be achieved by using cpprestsdk ("Casablanca").

Example:

using namespace web;
using namespace web::http;
using namespace web::http::client;
using namespace web::json;

void testFunction(http_request req)
{

   // only to test the serialization of json arrays
   json::value elmnt1;
   elmnt1[L"element"] = json::value::string(U("value1"));

   json::value elmnt2;
   elmnt2[L"element"] = json::value::string(U("value2"));

   json::value response;                     // the json array
   response[0] = elmnt1;
   response[1] = elmnt2;

   string outputStr = utility::conversions::to_utf8string(shifts.serialize());

   req.reply(200, outputStr, "application/json");
};

And this results in

[  
  {  
     "element":"value1"
  },
  {  
     "element":"value2"
   }
]

Upvotes: 1

sehe
sehe

Reputation: 393174

Boost documentation for JSON support is only a few lines:

http://www.boost.org/doc/libs/1_63_0/doc/html/property_tree/parsers.html#property_tree.parsers.json_parser

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

  • JSON objects are mapped to nodes. Each property is a child node.
  • JSON arrays are mapped to nodes. Each element is a child node with an empty name. If a node has both named and unnamed child nodes, it cannot be mapped to a JSON representation.
  • JSON values are mapped to nodes containing the value. However, all type information is lost; numbers, as well as the literals "null", "true" and "false" are simply mapped to their string form.
  • Property tree nodes containing both child nodes and data cannot be mapped.

JSON round-trips, except for the type information loss.

Highlighting mine

Upvotes: 3

Related Questions