Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Gson with Interface Types
    primarykey
    data
    text
    <p>I am working on some server code, where the client sends requests in form of JSON. My problem is, there are a number of possible requests, all varying in small implementation details. I therefore thought to use a Request interface, defined as:</p> <pre><code>public interface Request { Response process ( ); } </code></pre> <p>From there, I implemented the interface in a class named <code>LoginRequest</code> as shown:</p> <pre><code>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 + "]"; } } </code></pre> <p>To work with JSON, I created a <code>GsonBuilder</code> instance and registered an <code>InstanceCreator</code> as shown:</p> <pre><code>public class LoginRequestCreator implements InstanceCreator&lt;LoginRequest&gt; { @Override public LoginRequest createInstance(Type arg0) { return new LoginRequest("username", "password"); } } </code></pre> <p>which I then used as shown in the snippet below:</p> <pre><code>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); </code></pre> <p>and I get the expected output.</p> <p>The thing I wish to do is replace the line <code>Request request = parser.fromJson(completeInput, LoginRequest.class);</code> with something similar to <code>Request request = parser.fromJson(completeInput, Request.class);</code> but doing that will not work, since <code>Request</code> is an interface.</p> <p>I want my <code>Gson</code> to return the appropriate type of request depending on the received JSON.</p> <p>An example of the JSON I passed to the server is shown below:</p> <pre><code>{ "type":"LOGIN", "username":"someuser", "password":"somepass" } </code></pre> <p>To reiterate, I am looking for a way to parse requests (In JSON) from clients and return objects of classes implementing the <code>Request</code> interface</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload