Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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"));
}
share|improve this question
    
Is there a reason you cannot use a map/dictionary from component name to a function/delegate/etc that creates the desired type of component (basically a factory? – Josh Petrie Aug 2 at 19:52
    
I'm more or less wondering if there is an equivalent to SnakeYAML's method of doing this. – Beautiful Chaos Aug 2 at 19:53

So are you asking how to create a class given a class type as a string?

JsonReader reader = new JsonReader();
JsonValue base = reader.parse("components.json");

for (JsonValue component : base.get("components")) {
    Class<?> clazz = Class.forName(component.getString("class"));
    Constructor<?> ctor = clazz.getConstructor();
    Object object = ctor.newInstance();
}

Though I assume you probably have no arg consructors, if you need to pass arguments to a constructor, it is done like this (if the argument was a String):

Class<?> clazz = Class.forName(className);
Constructor<?> ctor = clazz.getConstructor(String.class);
Object object = ctor.newInstance(new Object[] { ctorArgument });
share|improve this answer
    
Which is appropriate for deserializing JSON into known objects. It won't work, however, if I want to deserialize an array of components to be stored in a collection such as ArrayList<? extends Component>, such that I don't know the exact type of component I am deserializing. – Beautiful Chaos Aug 3 at 1:00
    
My bad. I updated my answer with attempt #2 – spectacularbob Aug 3 at 16:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.