0
public <T> void getJsonObject(String res, RequestCallBack<T> callBack) {
    T result;
    if (res != null) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            result = mapper.readValue(res, new TypeReference<T>() {});
            callBack.onSuccess(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

it can't transmit T to the mapper the result is not what i want, what should I do?

H eyDacy
  • 3
  • 2

1 Answers1

0

A lot of libraries get around the type erasure by mandating the caller to pass the class in.

public <T> void getJsonObject(String res, RequestCallBack<T> callBack, Class<T> clazz) {
    T result;
    if (res != null) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            result = mapper.readValue(res, clazz);
            callBack.onSuccess(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

On a side note, you probably shouldn't be creating a new ObjectMapper for each call (it can be an expensive operation). I would suggest creating a single static one.

Jon Peterson
  • 2,966
  • 24
  • 32
  • Thank you , I used another method to solve it, I deserialize the string in the RequestCallback so i can get the type of T and deserialize it. – H eyDacy Aug 26 '16 at 03:52