I am using different functions and when executing each function, I would like to create Json message for communication like as shown below:
TestFunction1(string id)
{
"message" : "MSG_TestFunction1",
"id" : "1212"
}
TestFunction2(string id)
{
"message" : "MSG_TestFunction2",
"id" : "1213"
}
In this scenario, i am thinking that it doesn't require to maintain a .JSON file as we can create the json messages in the functions itself (like in TestFunction1 and TestFunction2 etc).
By considering all these i created the below sample using BOOST library with write_json and read_json.
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/foreach.hpp>
#include<string>
using namespace boost::property_tree;
using namespace std;
bool TestFunction1(std::string f_id)
{
ptree strTree;
ptree subject_info;
ptree array1;
array1.put("message", "MSG_TestFunction1");
array1.put("id", f_id);
subject_info.push_back(make_pair("", array1));
stringstream s;
write_json(s, array1);
string outstr = s.str();
stringstream stream(outstr);
ptree strreadTree;
try {
read_json(stream, strreadTree);
}
catch (ptree_error& e) {
return false;
}
return true;
}
int main()
{
TestFunction1("1212");
system("pause");
return 0;
}
Is this the correct way to create and parse the json data? Also Please help me how to create a generic function or a class with write and read json to make use of all the functions like TestFunction1, TestFunction2 etc to create and parse the json data.
Thanks in advance