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
- Optional (Feature):
std::optional<T>support — JSON null maps to nullopt without error (requires C++17)
- Download folder: include
- Add include line
#include "AIGCJson.hpp" - Add class-members registered line
AIGC_JSON_HELPER(xxx,yyy,zzz)
| Category | C++ Type | JSON |
|---|---|---|
| Primitives | int8_t、uint8_t、int16_t、uint16_t |
number |
int32_t、uint32_t、int64_t、uint64_t |
number | |
int、unsigned int、short、unsigned short |
number | |
long、unsigned long、long long、unsigned long long |
number | |
float、double |
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 |
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-failureIntegrate 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| 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.
#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.cppand 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;
}