简介
使用rapidjson库进行封装,实现了使用C++对结构体数据和json字符串进行互相转换的功能。最短只需要使用两行代码即可无痛完成结构体数据转换为Json字符串。
支持std::string、数组、POD数据(int,float,double等)、std::vector、嵌套结构体,容器内放结构体等。
优点
1、代码轻量级;仅需引入rapidjson库以及三个头文件即可完成;
2、操作简单;仅需在结构体声明位置注册结构体类型和成员变量;使用时使用提供的序列化和反序列化类接口即可完成转化;使用成员变量名为Json的Key值。
使用示例:
#pragma once
#include <vector>
#include "JsonStructPackage/JsonStructPackageHeader.h"struct TestStructQQQ
{int32_t intTest;short shortTest;long longTest;long long longLongTest;float floatTest;int32_t unsignedIntTest;double doubleTest;unsigned long long unsignedLongLongTest;std::string tsttr;
};
JSONTRANSLATE(TestStructQQQ, intTest, shortTest, longTest, longLongTest, floatTest, unsignedIntTest, doubleTest, unsignedLongLongTest, tsttr);struct TestStructA
{int32_t ab;std::string str;double doubleValue;float floatValue;TestStructQQQ qqq;char testChar[64];std::vector<TestStructQQQ> testVectorStruct;std::vector<int32_t> testVectorInt;std::vector<std::string> testVectorStr;TestStructA(){memset(testChar, 0, sizeof(testChar));}
};
JSONTRANSLATE(TestStructA, ab, str, doubleValue, floatValue, qqq, testChar, testVectorStruct, testVectorInt, testVectorStr);int32_t main(int32_t argc, char* argv[])
{TestStructQQQ testQQQ;testQQQ.intTest = 999;testQQQ.shortTest = 32765;testQQQ.longTest = 65536789;testQQQ.longLongTest = 665565345;testQQQ.floatTest = 12345.4567;testQQQ.unsignedIntTest = 7678;testQQQ.doubleTest = 456.67867876;testQQQ.unsignedLongLongTest = 1232345678;testQQQ.tsttr = "This is TestString!哈哈";TestStructA testStruct;testStruct.ab = 99;testStruct.doubleValue = 123.652432;testStruct.floatValue = 99.987;testStruct.str = "This is a String";testStruct.qqq = testQQQ;testStruct.testVectorStruct.emplace_back(testQQQ);testStruct.testVectorStruct.emplace_back(testQQQ);testStruct.testVectorInt.emplace_back(99);testStruct.testVectorInt.emplace_back(55);testStruct.testVectorStr.emplace_back("String1");testStruct.testVectorStr.emplace_back("String2");testStruct.testChar[0] = 'T';testStruct.testChar[1] = 'E';testStruct.testChar[2] = 'S';testStruct.testChar[3] = 'T';/**************核心-结构体转Json****************************/JsonStructPackager jsonPackage;jsonPackage << testStruct;std::string resJson = jsonPackage.data();/**************核心-结构体转Json****************************//**************核心-Json转结构体****************************/JsonStructUnpackager jsonUnpackage(resJson);TestStructA unpackageStruct;jsonUnpackage >> unpackageStruct;/**************核心-Json转结构体****************************/return 0;
}
resJson输出如下(经过json格式化后的,直接输出是一个没有换行的字符串):
resJson : {"ab": 99,"str": "This is a String","doubleValue": 123.652432,"floatValue": 99.98699951171875,"qqq": {"intTest": 999,"shortTest": 32765,"longTest": 65536789,"longLongTest": 665565345,"floatTest": 12345.45703125,"unsignedIntTest": 7678,"doubleTest": 456.67867876,"unsignedLongLongTest": 1232345678,"tsttr": "This is TestString!哈哈"},"testChar": "TEST","testVectorStruct": [{"intTest": 999,"shortTest": 32765,"longTest": 65536789,"longLongTest": 665565345,"floatTest": 12345.45703125,"unsignedIntTest": 7678,"doubleTest": 456.67867876,"unsignedLongLongTest": 1232345678,"tsttr": "This is TestString!哈哈"},{"intTest": 999,"shortTest": 32765,"longTest": 65536789,"longLongTest": 665565345,"floatTest": 12345.45703125,"unsignedIntTest": 7678,"doubleTest": 456.67867876,"unsignedLongLongTest": 1232345678,"tsttr": "This is TestString!哈哈"}],"testVectorInt": [99,55],"testVectorStr": ["String1","String2"]
}