简体   繁体   中英

Deserializing an Interface in GSON

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.

Verify controller.TopDownGridMovement has a no-arg constructor. If it does not then you cannot create an instance of the class using:

return (Movement) clazz.newInstance();

Ah... I had previously been creating a new Gson instance wherever I needed one, but recently created a GsonInstance singleton that I registered my custom serializers with. The Gson object that the Movement object was being deserialized from was not the GsonInstance object, so Gson didn't know how to serialize it I guess. Silly mistake.

tl;dr: I hadn't registered the custom serializer with the particular Gson instance I was using.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM