I’m having an issue with Java types and Gson, when being called from Kotlin
The Java POJO’s representing an API response and it’s errors
public class ApiResponse {
@SerializedName("errors")
public List<ApiResponseError> errors;
}
public class ApiResponseError {
@SerializedName("code")
public Integer code;
@SerializedName("message")
public String message;
}
The http response body on the wire
{
"errors": [
{
"code": 10002,
"message": "Not found"
}
]
}
The kotlin code using Gson to parse the response into objects.
val errorResponse = Gson().fromJson<ApiResponse>(jsonBody, ApiResponse::class.java)
jsonBody
is the string representation of the http response body on the previous snippet.
The debugger view of instantiated objects:
The problem is that the ArrayList
should contain a ApiResponseError
object and not a LinkedTreeMap
.
I tried the same code without the generic type on the List<ApiResponseError> errors
list, and the result is the same, so I am guessing the generic information is being lost somewhere and Gson is defaulting to LinkedTreeMap
.
Any clues or references I can take a look?
Thanks