Couldn't think of better phrasing for this question, but basically I want to store the name of a concrete class in GSON (see "movement"):
{
"player" : {
"position" : { "x" : 300, "y" : 400 },
"scale" : 1,
"rotation" : 0,
"sprite" : {
"frames" : [ {
"fileName" : "data/plane.png"
} ],
"duration" : 1
}
},
"movementDelay" : { "elapsed" : 0, "started" : 0, "delay" : 150 },
"movement" : { "class" : "controller.TopDownGridMovement" }
}
This is the class that contains the Movement interface I want to deserialize:
public class PlayerController {
private Player player;
private DelayTimer movementDelay;
private Movement movement;
public PlayerController() {
}
[...]
}
I wrote a Custom Deserializer:
package gson;
import java.lang.reflect.Type;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import controller.Movement;
public class MovementDeserialiser implements JsonDeserializer<Movement> {
@Override
public Movement deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
try {
JsonObject obj = (JsonObject) json;
Class clazz = Class.forName(obj.get("class").getAsString());
return (Movement) clazz.newInstance();
} catch (Exception e) {
throw new JsonParseException(e.getMessage());
}
}
}
I registered the deserializer:
public void registerAdapters(GsonBuilder gsonBuilder) {
gsonBuilder.registerTypeAdapter(Image.class, new ImageDeserialiser());
gsonBuilder.registerTypeAdapter(Button.class, new ButtonDeserialiser());
gsonBuilder.registerTypeAdapter(Animation.class, new AnimationDeserialiser());
gsonBuilder.registerTypeAdapter(Movement.class, new MovementDeserialiser());
}
Then I tried to deserialize the class that contains the Movement interface:
playerController = gson.fromJson(new FileReader("data/player_data/player_data.json"), PlayerController.class);
But I get this error:
ERROR:Unable to invoke no-args constructor for interface controller.Movement. Register an InstanceCreator with Gson for this type may fix this problem.
What do I need to do to get this to work? The idea of specifying a class to load came from Spring's bean config stuff - not sure if there's a better way to do it.
Oh, and from the reading around that I did, I determined that I shouldn't have to create an InstanceCreator for Movement. I'm providing a custom deserializer, after all...
Cheers.