@Override
public Response<?> deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
if (!(json instanceof JsonObject)) {
throw new JsonParseException("JonObject expected, found "
+ json.getClass().getSimpleName());
}
JsonObject jObject = (JsonObject) json;
if (!jObject.has(JSON_RPC_PROPERTY)) {
throw new JsonParseException(
"Invalid JsonRpc response lacking version '"
+ JSON_RPC_PROPERTY + "' field");
}
if (!jObject.get(JSON_RPC_PROPERTY).getAsString()
.equals(JsonRpcConstants.JSON_RPC_VERSION)) {
throw new JsonParseException("Invalid JsonRpc version");
}
Integer id;
try {
id = Integer.valueOf(jObject.get(ID_PROPERTY).getAsInt());
} catch (Exception e) {
throw new JsonParseException(
"Invalid JsonRpc response. It lacks a valid '"
+ ID_PROPERTY + "' field");
}
if (jObject.has(RESULT_PROPERTY)) {
ParameterizedType parameterizedType = (ParameterizedType) typeOfT;
return new Response<>(id, context.deserialize(
jObject.get(RESULT_PROPERTY),
parameterizedType.getActualTypeArguments()[0]));
} else if (jObject.has(ERROR_PROPERTY)) {
return new Response<>(id, (ResponseError) context.deserialize(
jObject.get(ERROR_PROPERTY), ResponseError.class));
} else {
throw new JsonParseException("Invalid JsonRpc response lacking '"
+ RESULT_PROPERTY + "' and '" + ERROR_PROPERTY
+ "' fields. " + json);
}
}