How to create a generic function with write_json and read_josn using C++ boost library

Viewed 133

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

1 Answers

Please, do not use Property Tree as a JSON library.

Boost 1.75.0 added a JSON library though!

As the other commenter said, it's not very clear what exactly you require, so let me just sketch a few similar use-cases:

#include <boost/json.hpp>
#include <boost/json/src.hpp> // for compiler explorer
#include <string>
#include <iostream>

namespace json = boost::json;

json::value TestFunction(std::string const id) {
    return json::object{
        {"message", "MSG_TestFunction1"},
        {"id", id}
    };
}

int main() {
    for (auto id : {"1212", "1213"}) {
        json::error_code ec;
        auto jsonString = serialize(TestFunction(id));
        auto roundtrip = json::parse(jsonString);

        std::cout << roundtrip << std::endl;
    }
}

Prints Live On Compiler Explorer

{"message":"MSG_TestFunction1","id":"1212"}
{"message":"MSG_TestFunction1","id":"1213"}

BONUS

Note that, unlike Property Tree, you have types, which means the IDs don't have to be strings:

for (auto id : {1212, 1213}) {
    std::cout << json::object{{"message", "MSG_TestFunction1"}, {"id", id}}
              << std::endl;
}

Prints (Live Again):

{"message":"MSG_TestFunction1","id":1212}
{"message":"MSG_TestFunction1","id":1213}
Related