I am looking for a way to load a specific class/subclass type based on JSON input. For example, I have the following JSON file
"type" : "RenderVertices"
"RenderComponent": {
"vertex": {
"xcoords" : [0, 1, 1, 0]
"ycoords" : [0, 0, 1, 1]
}
"texture": {
"name" : "testtex"
"texcoordsx" : [0, 1, 1, 0]
"texcoordsy" : [0, 0, 1, 1]
}
"color" : {
"color_r": [0, 0, 0, 0]
"color_g": [0, 0, 0, 0]
"color_b": [0, 0, 0, 0]
"color_a": [0, 0, 0, 0]
}
"type": "quads"
}
In my program I have two classes, a base class and a subclass
class RenderComponent;
and
class RenderVertices : public RenderComponent
I will eventually have a large number of classes that inherit from RenderComponent, and I would like be able to construct a specific object subclass (in this case, RenderVertices) based on the "type" specifier in the JSON file. I understand how to do this using some sort of factory function with a switch statement, but this requires adding a new entry for every subclass type I make, which I would like to avoid if possible. What is the most scalable way to accomplish this (and by scalable I mean least required code modifications per new subclass created)?
I am using nlohmann::json to parse my JSON file (https://github.com/nlohmann/json)