Skip to content

wisecubeagain/AIGCJson

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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
  6. Optional (Feature): std::optional<T> support — JSON null maps to nullopt without error (requires C++17)

📺 Use

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

Supported Data Types

Category C++ Type JSON
Primitives int8_tuint8_tint16_tuint16_t number
int32_tuint32_tint64_tuint64_t number
intunsigned intshortunsigned short number
longunsigned longlong longunsigned long long number
floatdouble number
bool true/false
std::string string
STL containers std::vector<T>std::list<T> array
std::set<T>std::unordered_set<T> array
std::map<std::string,T>std::unordered_map<std::string,T> object
Custom types Classes/structs with AIGC_JSON_HELPER object (nested)
Derived classes with AIGC_JSON_HELPER_BASE inheritance
enum as int
Optional feature std::optional<T> (requires AIGCJSON_FEATURE_OPTIONAL=ON) null or T

🔨 CMake Build and Integration

This project provides CMake support for building tests and integrating into other projects.

Build and run tests:

cd AIGCJson
mkdir build && cd build
cmake .. -DAIGCJSON_BUILD_TESTS=ON
cmake --build .
ctest --output-on-failure

Integrate into your CMake project (recommended):

add_subdirectory(path/to/AIGCJson)
target_link_libraries(your_target PRIVATE AIGCJson)

No separate rapidjson dependency is needed; headers are included under include/rapidjson. Use -DAIGCJSON_BUILD_TESTS=OFF if you do not want to build tests.

Enable optional features (e.g. std::optional, requires C++17):

cmake .. -DAIGCJSON_FEATURE_OPTIONAL=ON

Optional Features

Feature Description Requirement CMake option
std::optional JSON null maps to nullopt without error C++17 -DAIGCJSON_FEATURE_OPTIONAL=ON

Uses C++11 by default; enabling optional upgrades to C++17. More features may be added later.

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

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • C++ 93.9%
  • C 5.7%
  • CMake 0.4%