I am using RestAssured framework for api calls and I want to create a method that will return give me value from the JSON response in the specific variable type.
I want to use generic return type but I don't like me implementation.
The RestAssured getList() returns a list of values according to JSON path and cast to value type.
When I implement the getList() method, I have to cast the result to T in order to comply to the return value type:
public <T> T getValuesFromResponse(Response response, ValueTypeEnum value) {
T t = null;
switch (value) {
case ID:
//Will return List<Integer>
t = (T) response.thenReturn().jsonPath().getList("data.id", Integer.class);
break;
case NAME:
//Will return List<String>
t = (T) response.thenReturn().jsonPath().getList("data.name", String.class);
break;
}
return t;
}
When I want to call this method, I have to cast the values again to the type I need, i.e:
((List<Integer>) getValuesFromResponse(response, ValueTypeEnum.ID)).get(0)
How do I avoid the double casting?
Is my implementation correct for using generic List<T>
return type?