/AIGCJson

Only need two lines of code to convert between class and json. 两行代码实现C++ Json与类对象相互转换。

Primary LanguageC++MIT LicenseMIT

ENGLISH | 中文文档

AIGCJson

AIGCJson

AIGCJson is a tool for converting between classes and Json,which supports multiple data types and nested relationship.Only header file.(Depend onTencent/rapidjson

🍟 Support

  1. 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
  2. Supports nested relationship
  3. Only need two lines of code to convert
  4. Support rename class-members
  5. Support set default value

📺 Use

  1. Download folder: include
  2. Add include line #include "AIGCJson.hpp"
  3. Add class-members registered line AIGC_JSON_HELPER(xxx,yyy,zzz)

🤖 Example

#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

💻 Debug and Expand

Debug

  1. Downlad and install VSCodeMinGW
  2. Download this repository and open by vscode
  3. Select debug option: “windows g++” (“linux g++" if in linux)
  4. Open test.cpp and press F5

Expand

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