How to convert class object to json string using boost library in C++?
Asked Answered
H

3

7

I am fairly new to C++ and I apologise beforehand if you find this very easy.

I have the following files POST1.h

#ifndef POST1_HH
#define POST1_HH

#include <iostream>

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

using namespace std ;
using boost::property_tree::ptree;
using boost::property_tree::read_json;
using boost::property_tree::write_json;
using boost::property_tree::basic_ptree;

#include "DBAccess2.h"

class POST1
{

    public:
    string TokenNo;
    string CommandStatus;
    string CommandID;
    string CPUID;
    string ISEncrypted;
    string JSON_Cmnd_String;

    void POST_Device_Status(sqliteDB & DB_OBJ);

};

#endif

Below is POST1.cpp

#include <iostream>
#include <sstream>

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

#include "DBAccess2.h"
#include "POST1.h"

using namespace std ;
using boost::property_tree::ptree;
using boost::property_tree::read_json;
using boost::property_tree::write_json;
using boost::property_tree::basic_ptree;

void POST1::POST_Device_Status(sqliteDB & DB_OBJ)
{
    POST1 POST_OBJ;

    POST_OBJ.TokenNo = "1122";
    POST_OBJ.CommandStatus = "0";
    POST_OBJ.CommandID = "00";
    POST_OBJ.CPUID = "A1234B1234";
    POST_OBJ.ISEncrypted = "0";
    POST_OBJ.JSON_Cmnd_String = DB_OBJ.dump(DB_OBJ);
}

NOTE:-

(1) sqliteDB is another class declared in a .cpp file.

(2) the output of function dump() is a json string. this get stored into JSON_Cmnd_string.

So, I want to convert the class object into JSON string, How can I do that ? Do I have to first put these object into a container (like vector or list) and then write it into JSON?

Hygroscopic answered 31/3, 2015 at 7:50 Comment(2)
"Do I have to first put these object into a container (like vector or list) and then write it into JSON?" - this does not work out with the fact that you have multiple earlier questions using Boost Property Tree. Are you using SO as a code writing service?Gross
@Gross : Sorry sehe and everyone else. Next time I will be very specific and limited in asking problems after working thoroughly on them and I will base my question on particular part of problem only rather than whole code. I truly apologise.Hygroscopic
G
7

This is not "fairly easy", because C++ doesn't have JSON support.

Neither does Boost:

That said, this appears to be what you want:

So, I want to convert the class object into JSON string, How can I do that ? Do I have to first put these object into a container (like vector or list) and then write it into JSON?

Yes, you put them into a tree container, namely boost::property_tree::ptree:

Live On Coliru

#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <iostream>
#include <sstream>
using boost::property_tree::ptree;

namespace Entities {

    struct POST1 {
        std::string TokenNo;
        std::string CommandStatus;
        std::string CommandID;
        std::string CPUID;
        std::string ISEncrypted;

    };

    std::string to_json(POST1 const& o) {
        ptree out;
        out.put("POST1.TokenNo",          o.TokenNo);
        out.put("POST1.CommandStatus",    o.CommandStatus);
        out.put("POST1.CommandID",        o.CommandID);
        out.put("POST1.CPUID",            o.CPUID);
        out.put("POST1.ISEncrypted",      o.ISEncrypted);

        std::ostringstream oss;
        boost::property_tree::write_json(oss, out);
        return oss.str();
    }
}

// ADL trigger; `using Entities::to_json` would be roughly equivalent, but not
// make it clear that ADL is happening
void to_json();

int main() {
    Entities::POST1 obj { "1122", "0", "00", "A1234B1234", "0" };
    std::cout << to_json(obj);
}

Output:

{
    "POST1": {
        "TokenNo": "1122",
        "CommandStatus": "0",
        "CommandID": "00",
        "CPUID": "A1234B1234",
        "ISEncrypted": "0"
    }
}
Gross answered 31/3, 2015 at 8:33 Comment(0)
N
1

with boost 1.78.0,you can use this

Neuro answered 27/11, 2021 at 16:56 Comment(0)
D
-1
use this simple way

pt::ptree root;


    root.put("POST1 .TokenNo", "1122");
    root.put("POST1 .CommandStatus", "0");
    root.put("POST1 .CommandID", "00");
    root.put("POST1 .CPUID", "A1234B1234");
    root.put("POST1 .ISEncrypted", "0");
    // Once our ptree was constructed, we can generate JSON on standard output

    pt::write_json(std::cout, root);

OUT PUT

{ "POST1": { "TokenNo": "1122", "CommandStatus": "0", "CommandID": "00", "CPUID": "A1234B1234", "ISEncrypted": "0" } }

Disprize answered 17/3, 2018 at 12:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.