1. 程式人生 > 其它 >Cpp serialize class in vector collection and define the serialize method in class via jsoncpp

Cpp serialize class in vector collection and define the serialize method in class via jsoncpp

//Model/Book.cpp
#include <iostream>
#include <jsoncpp/json/json.h>

using namespace std;
class Book
{
public:
    int Idx;
    double Id;
    char *Abstract;
    char *Author;
    char *Comment;
    char *Content;
    char* ISBN;
    char *Name;
    char *Title;
    char *Topic;

    void serializeBook(Json::Value &root)
    {
        root[
"Idx"]=Idx; root["Id"]=Id; root["Abstract"]=Abstract; root["Author"]=Author; root["Comment"]=Comment; root["Content"]=Content; root["ISBN"]=ISBN; root["Name"]=Name; root["Title"]=Title; root["Topic"]=Topic; free(Abstract);
free(Author); free(Comment); free(Content); free(ISBN); free(Name); free(Title); free(Topic); } };
//Model/Util.h
#include <chrono>
#include <ctime>
#include <dirent.h>
#include <fstream>
#include <iostream>
#include <thread>
#include 
<unistd.h> #include <uuid/uuid.h> #include <vector> #include "Model/Book.cpp" using namespace std; class Util { public: static char* uuidValue; static char* dtValue; static uint64_t readNum; char* getTimeNow(); char* getUuid(); void getBookVector(vector<Book> &vec,int len); void serializeVector(int len); }
//Model/Util.cpp
#include "Model/Util.h"

char* Util::dtValue=(char*)malloc(20);
char* Util::uuidValue=(char*)malloc(40);
uint64_t Util::readNum=0;

char *Util::getTimeNow()
{
    time_t rawTime=time(nullptr);
    struct tm tmInfo=*localtime(&rawTime);
    strftime(dtValue,20,"%Y%m%d%H%M%S",&tmInfo);
    return dtValue;
}

char *Util::getUuid()
{
    uuid_t newUUID;
    uuid_generate(newUUID);
    uuid_unparse(newUUID,uuidValue);
    return uuidValue;
}

void Util::getBookVector(vector<Book> &vec,int len)
{
    for(int i=0;i<len;i++)
    {
        Book bk;
        bk.Idx=i;
        bk.Id=(double)i*i*i*i*i;
        bk.Abstract=(char*)malloc(40);
        bk.Author=(char*)malloc(40);
        bk.Comment=(char*)malloc(40);
        bk.Content=(char*)malloc(40);
        bk.ISBN=(char*)malloc(40);
        bk.Name=(char*)malloc(40);
        bk.Title=(char*)malloc(40);
        bk.Topic=(char*)malloc(40);
        strcpy(bk.Abstract,getUuid());
        strcpy(bk.Author,getUuid());
        strcpy(bk.Comment,getUuid());
        strcpy(bk.Content,getUuid());
        strcpy(bk.ISBN,getUuid());
        strcpy(bk.Name,getUuid());
        strcpy(bk.Title,getUuid());
        strcpy(bk.Topic,getUuid());
        vec.push_back(bk);
    }
}

void Util::logFileMsg(string fileName,string msg)
{
    fstream wFile(fileName,ios::app);
    if(!wFile.is_open())
    {
        cout<<"Create or open "<<fileName<<" failed!"<<endl;
        return;
    }

    wFile<<msg<<endl;
    wFile.close();
    cout<<getTimeNow()<<",writing into "<<fileName<<" finished!"<<endl;
}

void Util::serializeVector(int len)
{
    chrono::time_point<chrono::high_resolution_clock> startTime,endTime;
    startTime=chrono::high_resolution_clock::now();
    vector<Book> vec;
    getBookVector(std::ref(vec),len);
    
    Json::Value root;
    for (auto &bk : vec)
    {
        Json::Value jsonBk;
        bk.serializeBook(std::ref(jsonBk));
        root.append(jsonBk);
    }

    Json::StyledWriter styledWriter;
    string jsonValue=styledWriter.write(root);  

    string fileName=getTimeNow();
    fileName=fileName.append(".txt");
    logFileMsg(fileName,jsonValue);
    endTime=chrono::high_resolution_clock::now();
    cout<<getTimeNow()<<",finished in "<<__FUNCTION__<<endl;
    cout<<len<<", serizlization time cost :"
    <<chrono::duration_cast<chrono::seconds>(endTime-startTime).count()<<" seconds,"
    <<chrono::duration_cast<chrono::milliseconds>(endTime-startTime).count()<<" mills,"
    <<chrono::duration_cast<chrono::microseconds>(endTime-startTime).count()<<" micros,"
    <<chrono::duration_cast<chrono::nanoseconds>(endTime-startTime).count()<<" nanos!!!"<<endl<<endl;
}
//main.cpp
#include "Model/Util.h"

int main(int args, char **argv)
{
    Util ul;
    ul.serializeVector(atoi(argv[1]));
    cout << "Finished in " << __FUNCTION__ << "," << __LINE__ << endl;
}

Compile via g++

g++ -g -std=c++2a -I. *.cpp ./Model/*.cpp -o h1 -ljsoncpp -luuid

Run

./h1 10000