I have a library (wrapper around nlohmann/json) that allows me to deserialize from JSON:
struct MyStruct {
int propertyA;
std::string propertyB;
std::vector<int> propertyC;
}
void from_json(const JSON::Node& json, MyStruct& s)
{
json_get(s.propertyA, "propertyA", json);
json_get(s.propertyB, "propertyB", json);
json_get(s.propertyC, "propertyC", json);
}
As you can see, there is a lot of boiler plate in these definitions. I'm using an ECS framework that has hundreds of components I would like to deserialize. I'm hoping to simplify it with a macro such as:
struct MyStruct {
int propertyA;
std::string propertyB;
std::vector<int> propertyC;
JSON(MyStruct, propertyA, propertyB, propertyC);
};
}
I'm aware of the oldschool __VA_ARGS__
approach with manually repeating the macro N times but I was hoping to avoid that with a more generic / modern method.
Is this possible with variadic templates? Is there a better way to get provide sort of syntactic sugar for this? I'm using a C++17 compiler.