CJStruct is a header-only C++20 library for JSON <-> struct mapping, powered by Boost.PFR’s aggregate reflection. Just call JsonToStruct or StructToJson- no manual serializers needed.
This is my first C++ test project, please keep this in mind before writing angry issues :)
- Automatic (de)serialization of:
- Built-in types (
int,double,bool,std::string, etc.) - STL containers:
std::vector,std::list,std::set,std::map,std::array - Tuples/pairs:
std::tuple,std::pair - Nested aggregate structs
- Built-in types (
- Single header (
CJStruct.h), no macros - C++20 (or newer) support
- A C++20-compliant compiler (G++/Clang/MSVC)
- Boost.PFR (part of Boost ≥1.68)
- Add directory
CJStructto your include path. - Include and use:
#include <fstream>
#include <string>
#include <boost/pfr.hpp>
#include <CJStruct/CJStruct.h>
struct Inner { int a; bool b; };
struct MyData {
std::vector<int> v;
std::map<std::string,double> m;
std::string name;
Inner nested;
};
int main() {
// 1) Read JSON file into a string
std::ifstream ifs("data.json");
std::string json((std::istreambuf_iterator<char>(ifs)),
std::istreambuf_iterator<char>());
// 2) Parse JSON → struct
MyData data{};
JsonParser::JsonToStruct<MyData>(json, data);
// ... modify data ...
// 3) Serialize struct → JSON
std::string out;
JsonParser::StructToJson<MyData>(out, data);
// 4) Write JSON out
std::ofstream ofs("out.json");
ofs << out;
}This project is released under the MIT License.