From e00b2ded590dacff526224e5f4acff7221955e0b Mon Sep 17 00:00:00 2001 From: MBlyzniukov Date: Thu, 13 Oct 2022 16:44:43 +0300 Subject: [PATCH] Fix Json parse exception --- .../twitter/helpers/AbstractRequestHelper.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/github/redouane59/twitter/helpers/AbstractRequestHelper.java b/src/main/java/io/github/redouane59/twitter/helpers/AbstractRequestHelper.java index 1c9c995c..4bc03ee4 100644 --- a/src/main/java/io/github/redouane59/twitter/helpers/AbstractRequestHelper.java +++ b/src/main/java/io/github/redouane59/twitter/helpers/AbstractRequestHelper.java @@ -1,11 +1,13 @@ package io.github.redouane59.twitter.helpers; +import com.fasterxml.jackson.core.JsonProcessingException; import com.github.scribejava.apis.TwitterApi; import com.github.scribejava.core.builder.ServiceBuilder; import com.github.scribejava.core.model.OAuthRequest; import com.github.scribejava.core.model.Response; import com.github.scribejava.core.model.Verb; import com.github.scribejava.core.oauth.OAuth10aService; +import io.github.redouane59.twitter.TwitterClient; import io.github.redouane59.twitter.signature.TwitterCredentials; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -101,7 +103,7 @@ public Optional makeRequest(OAuthRequest request, boolean signRequired, C } else if (response.getCode() < 200 || response.getCode() > 299) { logApiError(request.getVerb().name(), request.getUrl(), stringResponse, response.getCode()); } - result = JsonHelper.fromJson(stringResponse, classType); + result = convert(stringResponse, classType); } catch (IOException ex) { LOGGER.error("Error occupied on executing request", ex); } @@ -109,6 +111,14 @@ public Optional makeRequest(OAuthRequest request, boolean signRequired, C return Optional.ofNullable(result); } + protected T convert(String json, Class targetClass) throws JsonProcessingException { + if (targetClass.isInstance(json)) { + return (T) json; + } else { + return JsonHelper.fromJson(json, targetClass); + } + } + public abstract Optional getRequest(String url, Class classType); public abstract Optional getRequestWithParameters(String url, Map parameters, Class classType);