Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/archibate/reflect-hpp
- Owner: archibate
- Created: 2024-06-28T12:44:02.000Z (5 months ago)
- Default Branch: master
- Last Pushed: 2024-07-23T07:33:25.000Z (4 months ago)
- Last Synced: 2024-07-23T09:33:27.999Z (4 months ago)
- Language: C++
- Size: 11.7 KB
- Stars: 17
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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;
}
```