Created
September 27, 2017 20:48
-
-
Save eliasdaler/45bf3f583cd4a41019b9802c198e6f41 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <meta.h> | |
#include <json.h> | |
using json = nlohmann::json; | |
struct Entity { | |
int id; | |
}; | |
struct Character : Entity { | |
std::string name; | |
}; | |
namespace meta { | |
template <> | |
inline auto registerMembers<Entity>() | |
{ | |
return members( | |
member("id", &Entity::id) | |
); | |
} | |
template <> | |
inline auto registerMembers<Character>() | |
{ | |
return std::tuple_cat( | |
meta::getMembers<Entity>(), // will get all members from Entity | |
members(member("name", &Character::name)) | |
); | |
} | |
} | |
int main() | |
{ | |
Character c; | |
c.id = 42; | |
c.name = "Douglas"; | |
json j = c; | |
std::cout << std::setw(4) << j << std::endl; | |
/* | |
output: | |
{ | |
"id": 42, | |
"name": "Douglas" | |
} | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment