This week, we Aeste interns had much fun where our boss brought us for movie on Wednesday and a farewell lunch for Peter (bye Peter!) on Friday. As for me, this would be the second last blog post.

Using Boost::property_tree

Back on topic… I mentioned last week that I was using boost::regex to modify(edit values, delete nodes) json files. It was brain-damage inducing and very dumb. Luckily, Dr. Shawn read my blog and told me about this. Turns out that there is a boost::property_tree library that allows the creation and manipulation of JSON in such a simple way. There is however, little working example in the web, so I will share briefly how I used it to make my life easier. In fact, I think it’s easier to use compared to Wt::Json.

Let’s say we want to build ‘projectFiles’ key with an array value, where the array elements are objects:

{“title”: “MyProject-2015-12-11 11:14:13”},
{“title”: “MyProject-2015-12-11 18:45:41”}

The code:

boost::property_tree::ptree pt;
boost::property_tree::ptree completedChildren;
boost::property_tree::ptree newChild;
completedChildren.push_back(std::make_pair(“”, newChild));

This will create one array element, if we want to open the original json and attach another array element to ‘projectFiles’, this is my way:

boost::property_tree::ptree oldpt, oldptBoard;
boost::property_tree::ptree newChild;
boost::property_tree::read_json(jsonInput,oldpt); //read_json will accept stringstream and return the parsed result to a ptree
oldptBoard = oldpt.get_child(“projectFiles”); //the child of the old json is saved
newChild.put(“title”,title); //create a new child
oldptBoard.push_back(std::make_pair(“”, newChild));//push_back the new child with the old json child
pt2.put_child(“projectFiles”,oldptBoard); //put the child under ‘projectFiles’

As to editing the file, say we want to change the title, we would need to iterate through the array.

BOOST_FOREACH(boost::property_tree::ptree::value_type &v, pt.get_child(“projectFiles”)) {
v.second.put<std::string>(“title”,”newTitle”); //put will update the pt whereas add will create a new key

Using boost::property_tree, I can also delete one whole array element using just a few lines of code, which is muuuuchh easier compared to the caveman method I used previously.

Using WResource to Download/Upload

I was using Wt::Http::Client in my WApplication to upload and download files previously. Dr. Shawn told me what a bad idea it was and how it could be better by using only WResource. I am to build a stateless WResource which could be implemented without any dependence on other sessions. The product would even achieve better scalability since there is no need to store data.

During my transition to using WResource, I faced some challenges which I bothered Dr. Shawn quite some times. Thankfully, he showed me mercy. In WResource, I am using handleRequest() member function to process requests and return a response. I learned that the response must be returned immediately in the handleRequest() function, if not, we would not be able to respond to the request forever. This made what I wanted to implement difficult because:-

  1. I can’t return the response immediately until I obtain some extra information from another server (again using HTTP requests).
  2. HTTP requests using Wt::Http::Client are asynchronous in nature (done() signal is emitted when request is is done).
  3. Hence, I would never be able to return the response in the handleRequest() of WResource.

Instead of using Http::Client, Dr. Shawn said to use curl instead. That made things so much easier. Looking closely, it is a curl request to get a respond from another curl request, which is quite amusing for me.