1

Why this is not possible ?

Type type = newResponse.getDataType().getType();
Class<?> tClass = type.getClass();                
Type arrayType = new TypeToken<ArrayList<tClass>>(){}.getType();

Complete deserializer is as follows -

public class NewJsonDesrializer implements JsonDeserializer<NewResponse> {

    @Override
    public NewResponse deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
            throws JsonParseException {

        JsonObject jsonElement = json.getAsJsonObject();
        NewResponse newResponse = new Gson().fromJson(json, typeOfT);
        JsonElement data = jsonElement.get("data");

        if(data.isJsonArray()){
            Type type = newResponse.getDataType().getType();
            Class<?> tClass = type.getClass();

            Type arrayType = new TypeToken<ArrayList<tClass>>(){}.getType();

            newResponse.setData(new Gson().fromJson(data, arrayType));


        }else{
            newResponse.setData(new Gson().fromJson(data, newResponse.getDataType().getType()));
        }


        return newResponse;
    }
}

This throws error in deserializer above at line -

Error:(36, 54) error: cannot find symbol class tClass

Complete NewResponse class is as follows-

public class NewResponse<T> implements Serializable {
    private boolean status;
    private String message;
    private T data;
    private Constants.DataType dataType;

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Constants.DataType getDataType() {
        return dataType;
    }

    public void setDataType(Constants.DataType dataType) {
        this.dataType = dataType;
    }
}

Enum that returns Type is as follows -

 public enum DataType{
        @SerializedName("user")
        USER(User.class);

        Type type;

        DataType(Type type) {
            this.type = type;
        }

        public Type getType(){
            return type;
        }
    }

Json structure is as follows -

{  
   status:true,
   message:"Registration Complete.",
   dataType:"user",
   data:[  
      {  
         username:"[email protected]",
         email:"[email protected]",
         created_on:"1426663448",
         last_login:null,
         active:"1",
         first_name:"Sachin Gutte",
         last_name:"",
         company:null,
         phone:null,
         sign_up_mode:"GOOGLE_PLUS"
      },
      {  
         username:"administrator",
         email:"[email protected]",
         created_on:"1268889823",
         last_login:"1425373557",
         active:"1",
         first_name:"Admin",
         last_name:"istrator",
         company:"ADMIN",
         phone:"0",
         sign_up_mode:"SOMEAPP"
      }
   ]
}
4
  • 2
    You can only specify types in type parameters directly, not indirectly through a variable. But, since type parameters in Java are not preserved at runtime (at runtime, a List has no idea whether it's a list of Integer or something else), why would you need specify it through a variable? Commented Mar 20, 2015 at 10:28
  • @ErwinBolwidt Im using Gson to parse the data. Code is part of deserializer. Jsone sometimes return single object or list of various class types ie models I hav e. Will update question with details. Commented Mar 20, 2015 at 10:31
  • @ErwinBolwidt Please see the updated question. Json in question is also posted. Commented Mar 20, 2015 at 10:41
  • How does the update make a difference? Type arrayType = new TypeToken<ArrayList<tClass>>(){}.getType(); is a strange line. TypeToken doesn't know what type of ArrayList it is parametrized with - this information isn't kept at runtime. And why are you creating an anonymous subtype of TypeToken and then calling the getType() method? How would this be any different from not creating an anonymous subtype? Commented Mar 20, 2015 at 11:54

1 Answer 1

1

Sorry, I don't have enough reputation to comment.

You have to know the type by compile time, not runtime. Thats why it cannot be resolved. Have a look at this link: http://docs.oracle.com/javase/tutorial/java/generics/erasure.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.