Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/archibate/reflect-hpp

Single header static reflection library for C++14 and above
https://github.com/archibate/reflect-hpp

Last synced: 11 days ago
JSON representation

Single header static reflection library for C++14 and above

Awesome Lists containing this project

README

        

# reflect.hpp

```cpp
#include
#include "reflect.hpp"
#include "reflect_json.hpp"

struct Address {
std::string country;
std::string province;
std::string city;

void show() {
std::cout << "Country: " << country << '\n';
std::cout << "Province: " << province << '\n';
std::cout << "City: " << city << '\n';
}

std::string to_str() const {
return country + " " + province + " " + city;
}

static void test() {
std::cout << "static function test\n";
}

REFLECT(country, province, city, show, to_str, test);
};

struct Student {
std::string name;
int age;
Address addr;

REFLECT(name, age, addr);
};

int main() {
Student stu = {
.name = "Peng",
.age = 23,
.addr = {
.country = "China",
.province = "Shanghai",
.city = "Shanghai",
}
};

std::string binary = reflect_json::serialize(stu);
std::cout << binary << '\n';
auto stuDes = reflect_json::deserialize(binary);

std::cout << stuDes.name << '\n';
std::cout << stuDes.age << '\n';
std::cout << stuDes.addr.country << '\n';
std::cout << stuDes.addr.province << '\n';
std::cout << stuDes.addr.city << '\n';

auto vec = reflect_json::deserialize>(R"json([1, 2, 3])json");
std::cout << vec.at(0) << '\n';
std::cout << vec.at(1) << '\n';
std::cout << vec.at(2) << '\n';

return 0;
}
```