GithubHelp home page GithubHelp logo

cffjfz / aigcjson Goto Github PK

View Code? Open in Web Editor NEW

This project forked from aigmix/aigcjson

0.0 0.0 0.0 175 KB

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

License: MIT License

C++ 95.57% C 4.43%

aigcjson's Introduction

ENGLISH | 中文文档

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、int64\uint64、float、double、bool、string、list、vector、map<string,T>
  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;
}

aigcjson's People

Contributors

yaronzz avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.