cpp jsoncpp serialize vector of class

//Model/Util.h
#pragma once
#ifndef __Util_H__
#define __Util_H__
#include <fstream>
#include <functional>
#include <iostream>
#include <string.h>
#include <uuid/uuid.h>
#include <vector>
#include "Model/Book.h"

using namespace std;

class Util
{
public:
    static char *uuidValue;
    static char *dtValue;
    char *getUuidValue();
    char *getTimeNow();
    void getBookVector(vector<Book> &vec, int len);
    void logFile(string fileName, string msg);
    void vectorBookSerialize(vector<Book> &vec);
    void vectorSerializeDemo(int len);
};
#endif


//Model/Util.cpp
#include "Model/Util.h"

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

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

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;
}

void Util::getBookVector(vector<Book> &vec, int len)
{
    cout << getTimeNow() << ",start in " << __FUNCTION__ << endl;
    for (int i = 0; i < len; i++)
    {
        Book bk;
        bk.Idx = (uint64_t)i * 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.Summary = (char *)malloc(40);
        bk.Title = (char *)malloc(40);
        bk.Topic = (char *)malloc(40);
        strcpy(bk.Abstract, getUuidValue());
        strcpy(bk.Author, getUuidValue());
        strcpy(bk.Comment, getUuidValue());
        strcpy(bk.Content, getUuidValue());
        strcpy(bk.ISBN, getUuidValue());
        strcpy(bk.Name, getUuidValue());
        strcpy(bk.Summary, getUuidValue());
        strcpy(bk.Title, getUuidValue());
        strcpy(bk.Topic, getUuidValue());
        vec.push_back(bk);
    }
    cout << getTimeNow() << ",finished in " << __FUNCTION__ << endl;
}

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

    wFile.setf(std::ios_base::fixed);
    wFile << std::fixed << msg << endl;
    wFile.close();
    cout << getTimeNow() << ",finished in " << __FUNCTION__ << endl;
}

void Util::vectorBookSerialize(vector<Book> &vec)
{
    cout << getTimeNow() << ",start in " << __FUNCTION__ << endl;
    Json::Value root;
    int vecSize = vec.size();
    for (int i = 0; i < vecSize; i++)
    {
        Json::Value jbk;
        Book bk = vec[i];
        bk.serializeBook(std::ref(jbk));
        root.append(jbk);
    }

    Json::StyledWriter writer;
    string jsonValue = writer.write(root);
    string fileName(getTimeNow());
    fileName = fileName.append(".txt");
    logFile(fileName, jsonValue);
    cout << getTimeNow() << ",finished in " << __FUNCTION__ << endl;
}

void Util::vectorSerializeDemo(int len)
{
    cout << getTimeNow() << ",start in " << __FUNCTION__ << endl;
    vector<Book> vec;
    getBookVector(std::ref(vec), len);
    vectorBookSerialize(std::ref(vec));
    cout << getTimeNow() << ",finished in " << __FUNCTION__ << endl;
}

 

 

//Model/Book.h
#pragma once
#ifndef __Book_H__
#define __Book_H__

#include <iostream>
#include <jsoncpp/json/json.h>

using namespace std;

class Book
{
public:
    uint64_t Idx;
    double Id;

    char *Abstract;
    char *Author;
    char *Comment;
    char *Content;
    char *ISBN;
    char *Name;
    char *Summary;
    char *Title;
    char *Topic;

    void serializeBook(Json::Value &jBk);
};

#endif
//Model/Book.cpp
#include "Model/Book.h"

void Book::serializeBook(Json::Value &jBk)
{
    jBk["Idx"] = Json::Value::UInt64(Idx);
    jBk["Id"] = Id;
    jBk["Abstract"] = Abstract;
    jBk["Author"] = Author;
    jBk["Comment"] = Comment;
    jBk["Content"] = Content;
    jBk["ISBN"] = ISBN;
    jBk["Name"] = Name;
    jBk["Summary"] = Summary;
    jBk["Title"] = Title;
    jBk["Topic"] = Topic;
    free(Abstract);
    free(Author);
    free(Comment);
    free(Content);
    free(ISBN);
    free(Name);
    free(Summary);
    free(Title);
    free(Topic);
}

 

 

#include "Model/Util.h"

int main(int args, char **argv)
{
    Util ul;
    ul.vectorSerializeDemo(atoi(argv[1]));
}

 

Compile

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

 

Run

./h1 1000000

cpp jsoncpp serialize vector of class

 

原文链接: https://www.cnblogs.com/Fred1987/p/16953557.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    cpp jsoncpp serialize vector of class

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/401221

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月19日 上午9:10
下一篇 2023年4月19日 上午9:10

相关推荐