Używanie Gsona z typami interfejsów

Pracuję nad jakimś kodem serwera, gdzie klient wysyła żądania w postaci JSON. Mój problem polega na tym, że istnieje wiele możliwych wniosków, które różnią się małymi szczegółami implementacji. Dlatego pomyślałem, że użyję interfejsu żądania zdefiniowanego jako:

public interface Request {
    Response process ( );
}

Stamtąd zaimplementowałem interfejs w klasie o nazwieLoginRequest jak pokazano:

public class LoginRequest implements Request {
    private String type = "LOGIN";
    private String username;
    private String password;

    public LoginRequest(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * This method is what actually runs the login process, returning an
     * appropriate response depending on the outcome of the process.
     */
    @Override
    public Response process() {
        // TODO: Authenticate the user - Does username/password combo exist
        // TODO: If the user details are ok, create the Player and add to list of available players
        // TODO: Return a response indicating success or failure of the authentication
        return null;
    }

    @Override
    public String toString() {
        return "LoginRequest [type=" + type + ", username=" + username
            + ", password=" + password + "]";
    }
}

Aby pracować z JSON, stworzyłemGsonBuilder przykład i zarejestrowałInstanceCreator jak pokazano:

public class LoginRequestCreator implements InstanceCreator<LoginRequest> {
    @Override
    public LoginRequest createInstance(Type arg0) {
        return new LoginRequest("username", "password");
    }
}

które następnie użyłem zgodnie z poniższym fragmentem:

GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(LoginRequest.class, new LoginRequestCreator());
Gson parser = builder.create();
Request request = parser.fromJson(completeInput, LoginRequest.class);
System.out.println(request);

i otrzymuję oczekiwaną wydajność.

To, co chcę zrobić, to wymienić linięRequest request = parser.fromJson(completeInput, LoginRequest.class); z czymś podobnym doRequest request = parser.fromJson(completeInput, Request.class); ale to nie zadziała, ponieważRequest jest interfejsem.

chcę mojeGson zwrócić odpowiedni rodzaj żądania w zależności od otrzymanego JSON.

Przykład JSON I przekazany do serwera pokazano poniżej:

{
    "type":"LOGIN",
    "username":"someuser",
    "password":"somepass"
}

Powtarzam, szukam sposobu na analizowanie żądań (w JSON) od klientów i zwracanie obiektów klas implementującychRequest berło

questionAnswers(4)

yourAnswerToTheQuestion