Skip to content

Instantly share code, notes, and snippets.

@eliasdaler
Created September 27, 2017 20:48
Show Gist options
  • Save eliasdaler/45bf3f583cd4a41019b9802c198e6f41 to your computer and use it in GitHub Desktop.
Save eliasdaler/45bf3f583cd4a41019b9802c198e6f41 to your computer and use it in GitHub Desktop.
#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