AIGCJson is a tool for converting between classes and Json,which supports multiple data types and nested relationship.Only header file.(Depend onTencent/rapidjson)
- Supports multiple data types, include int\uint、short\ushort、int64\uint64、float、double、bool、string、list、vector、map<string,T>、unordered_map<string,T>、set、unordered_set
- Supports nested relationship
- Only need two lines of code to convert
- Support rename class-members
- Support set default value
- Download folder: include
- Add include line
#include "AIGCJson.hpp"
- Add class-members registered line
AIGC_JSON_HELPER(xxx,yyy,zzz)
#include "AIGCJson.hpp"
using namespace std;
using namespace aigc;
class Student
{
public:
string Name;
int Age;
AIGC_JSON_HELPER(Name, Age) //class-members register
AIGC_JSON_HELPER_RENAME("name","age")//rename class-members
};
int main()
{
int age;
string jsonStr = R"({"name":"XiaoMing", "age":15})";
Student person;
JsonHelper::JsonToObject(person, R"({"name":"XiaoMing", "age":15})");
//get base-type or class from json string by keys
JsonHelper::JsonToObject(age, R"({"name":"XiaoMing", "age":15})", {"age"});
jsonStr = "";
JsonHelper::ObjectToJson(person, jsonStr);
return 0;
}
more example:test
- Downlad and install VSCode、MinGW
- Download this repository and open by vscode
- Select debug option: “windows g++” (“linux g++" if in linux)
- Open
test.cpp
and press F5
If you want to support other types, you just need to add two functions to the AIGCJson.hpp
,int-type example:
static bool JsonToObject(int &obj, rapidjson::Value &jsonValue)
{
if (jsonValue.IsNull() || !jsonValue.IsInt())
return false;
obj = jsonValue.GetInt();
return true;
}
static bool ObjectToJson(int &obj, rapidjson::Value &jsonValue, rapidjson::Document::AllocatorType &allocator)
{
jsonValue.SetInt(obj);
return true;
}