C++ 基于rapidjson对json字符串的进行序列化与反序列化
json字符串的解析以封装在我们开发过程中经常见到, 尤其在socket通信上面, 在一次项目中碰到json字符串的进行解析, 而公司有没有封装好的库, 于是就自己基于开源的库进行了一次封装, 接下是使用案例, 如果感觉可以, 就去下载我的源代码自己编译一个库出来(能直接编译通过, 无任何依赖),
下载地址: https://download.csdn.net/download/weixin_41370313/11457996
jsonObjDefine.h
#pragma once #include "../LibJosnSerial/JsonBase.h" #include <string> using namespace JsonBaseHelper; using namespace std; class Person : public JsonBase { public: string name; string sex; int age; int ident; void ToWrite(Writer<StringBuffer> &writer) { RapidjsonWriteBegin(writer); RapidjsonWriteString(name); RapidjsonWriteString(sex); RapidjsonWriteInt(age); RapidjsonWriteInt(ident); RapidjsonWriteEnd(); } void ParseJson(const Value& val) { RapidjsonParseBegin(val); RapidjsonParseToString(name); RapidjsonParseToString(sex); RapidjsonParseToInt(age); RapidjsonParseToInt(ident); RapidjsonParseEnd(); } }; class MacPropery : public JsonBase { public: int count; float perMachine; int machineCount; string operationTime; void ToWrite(Writer<StringBuffer> &writer) { RapidjsonWriteBegin(writer); RapidjsonWriteInt(count); RapidjsonWriteDouble(perMachine); RapidjsonWriteInt(machineCount); RapidjsonWriteString(operationTime); RapidjsonWriteEnd(); }; void ParseJson(const Value& val) { RapidjsonParseBegin(val); RapidjsonParseToInt(count); RapidjsonParseToDouble(perMachine); RapidjsonParseToInt(machineCount); RapidjsonParseToString(operationTime); RapidjsonParseEnd(); }; }; class PrintMacInfo : public JsonBase { public: int code; string message; JsonArray<MacPropery> data; void ToWrite(Writer<StringBuffer> &writer) { RapidjsonWriteBegin(writer); RapidjsonWriteInt(code); RapidjsonWriteString(message); RapidjsonWriteClass(data); RapidjsonWriteEnd(); }; void ParseJson(const Value& val) { RapidjsonParseBegin(val); RapidjsonParseToInt(code); RapidjsonParseToString(message); RapidjsonParseToClass(data); RapidjsonParseEnd(); }; };
测试demo
#include <iostream> #include "JsonObjDefine.h" //定义json对象头文件 #include <iostream> //基于rapidjson封装的库 #pragma comment(lib, "..\\LibJosnSerial\\Bin\\LibJosnSerial.lib") int main() { std::cout << "基础json字符串转换\n"; Person per; per.age = 99; per.ident = 123; per.name = "测试"; per.sex = "男性"; string strPerJson = JsonBase::SerializeJson(&per); std::cout << "Person -> Json字符串:" << std::endl << strPerJson << std::endl; std::cout << endl; std::cout << "Json字符串 -> Person" << std::endl; Person perRet; JsonBase::DeserializeJson(&perRet, strPerJson); cout << "age: " << perRet.age << endl; cout << "ident: " << perRet.ident << endl; cout << "name: " << perRet.name << endl; cout << "sex: " << perRet.sex << endl; std::cout << endl; std::cout << "有嵌套json字符串转换\n"; PrintMacInfo printMac; printMac.code = 0; printMac.message = "测试json字符串"; MacPropery mac_1; mac_1.count = 6031; mac_1.perMachine = 861.57; mac_1.machineCount = 7; mac_1.operationTime = "2012"; printMac.data.objList.push_back(mac_1); MacPropery mac_2; mac_2.count = 20754; mac_2.perMachine = 2964.86; mac_2.machineCount = 7; mac_2.operationTime = "2018"; printMac.data.objList.push_back(mac_2); string strJson = JsonBase::SerializeJson(&printMac); std::cout << "PrintMacInfo -> Json字符串:"<< std::endl << strJson<<std::endl; std::cout << endl; std::cout << "Json字符串 -> PrintMacInfo" << endl; PrintMacInfo printMacRet; JsonBase::DeserializeJson(&printMacRet, strJson); cout << "code:"<< printMacRet.code << endl; cout << "message" << printMacRet.message << endl; int index = 1; for each(auto it in printMacRet.data.objList) { cout << "data_MacPro_"<<index<<": "<< it.count << endl; cout << "data_MacPro_"<<index<<": "<< it.machineCount << endl; cout << "data_MacPro_"<<index<<": "<< it.perMachine << endl; cout << "data_MacPro_"<<index<<": "<< it.operationTime << endl; cout << endl; index++; } getchar(); }
打印输出:
普通字符串格式
{
"name": "测试",
"sex": "男性",
"age": 99,
"ident": 123
}
有嵌套json字符串格式
{
"code": 0,
"message": "测试json字符串",
"data": [{
"count": 6031,
"perMachine": 861.5700073242188,
"machineCount": 7,
"operationTime ": "2012 "
},
{
"count ": 20754,
"perMachine ": 2964.860107421875,
"machineCount ": 7,
"operationTime ": "2018 "
}
]
}