I have a collection of serialized components in JSON format:
{
"components": [{
"class": "com.package.component.PositionComponent",
"x": 100,
"y": 100
}, {
"class": "com.package.component.VelocityComponent",
"x": 4,
"y": 2
}]
}
I want to load this into a collection of base Components in Java, specifically into an Entity. I am currently using YAML (parsed using SnakeYAML) to achieve this. The following file (which is virtually identical to the JSON one) is parsed and the components are stored in an ArrayList:
components:
- ==: com.package.component.PositionComponent
x: 100
y: 100
- ==: com.package.component.VelocityComponent
x: 4
y: 2
Is there any way I can achieve this using the JSON parsing facilities provided by LibGDX? I am currently using the following code to parse the components, but I have no idea how to get it to construct the correct appropriate object for each component:
JsonReader reader = new JsonReader();
JsonValue base = reader.parse("components.json");
for (JsonValue component : base.get("components")) {
System.out.println(component.getString("class"));
}