mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 08:46:19 +08:00
e6044a85df646f861525403393c030071a7d1735
reflect-cpp
reflect-cpp is a C++-20 library for compile-time reflection, similar to serde in Rust, pydantic in Python or encoding in Go. As these libraries are among the most widely used libraries in the respective languages, reflect-cpp fills an important gap in C++ development.
Design principles for reflect-cpp include:
- Close integration with containers in the C++ library
- Close integration with C++ idioms
- Out-of-the-box support for JSON, no dependencies
- Simple installation: If no JSON support is required, reflect-cpp is header-only. For JSON support, only a single source file needs to be compiled.
- Simple extendability to other formats.
- No macros
#include <rfl>
#include <rfl/json>
// "firstName", "lastName" and "children" are the field names
// as they will appear in the JSON. The C++ standard is
// snake case, the JSON standard is camel case, so the names
// will not always be identical.
struct Person {
rfl::Field<std::string, "firstName"> first_name;
rfl::Field<std::string, "lastName"> last_name;
rfl::Field<std::vector<Person>, "children"> children;
};
const auto bart = Person{
.first_name = "Bart",
.last_name = "Simpson",
.children = std::vector<Person>()
};
const auto lisa = Person{
.first_name = "Lisa",
.last_name = "Simpson",
.children = rfl::default_value // same as std::vector<Person>()
};
// We only need to replace the first name to get from Lisa
// to Maggie.
const auto maggie = rfl::replace(
lisa, rfl::make_field<"firstName">("Maggie"));
const auto homer = Person{
.first_name = "Homer",
.last_name = "Simpson",
.children = std::vector<Person>({bart, lisa, maggie})
};
// We can now transform this into a JSON string.
const std::string json_string = rfl::json::write(homer);
// And we can retrieve the result.
const auto homer2 = rfl::json::read<Person>(json_string).value();
Installation
Using
Languages
C++
83%
C
15.5%
CMake
1.3%
Python
0.2%
