Skip to content
This repository has been archived by the owner on May 14, 2020. It is now read-only.

Commit

Permalink
#14 Json parsing moved to separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
axelerod committed Sep 29, 2015
1 parent e9bdc2e commit cc42d38
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@
*/
package com.smartling.api.sdk;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.smartling.api.sdk.dto.ApiResponse;
import com.smartling.api.sdk.dto.ApiResponseWrapper;
import com.smartling.api.sdk.dto.Data;
import com.smartling.api.sdk.util.DateTypeAdapter;
import com.smartling.api.sdk.util.HttpUtils;

import org.apache.commons.lang3.CharEncoding;
Expand All @@ -33,7 +30,6 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import static com.smartling.api.sdk.file.FileApiParams.API_KEY;
Expand Down Expand Up @@ -168,7 +164,7 @@ protected String buildParamsQuery(final NameValuePair... nameValuePairs)

protected List<NameValuePair> getRequiredParams()
{
final List<NameValuePair> qparams = new ArrayList<NameValuePair>();
final List<NameValuePair> qparams = new ArrayList<>();
qparams.add(new BasicNameValuePair(API_KEY, apiKey));
qparams.add(new BasicNameValuePair(PROJECT_ID, projectId));

Expand All @@ -185,7 +181,7 @@ protected List<BasicNameValuePair> getNameValuePairs(final String name, final Li
if (null == values || values.isEmpty())
return Collections.emptyList();

final List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>();
final List<BasicNameValuePair> nameValuePairs = new ArrayList<>();
for (final String value : values)
nameValuePairs.add(new BasicNameValuePair(name, value));

Expand All @@ -204,12 +200,6 @@ protected String getApiResponseMessages(final ApiResponse<?> apiResponse)

protected <T extends Data> ApiResponse<T> getApiResponse(final String response, final TypeToken<ApiResponseWrapper<T>> responseType)
{
final GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Date.class, new DateTypeAdapter());

final Gson gson = builder.create();
final ApiResponseWrapper<T> responseWrapper = gson.fromJson(response, responseType.getType());

return responseWrapper.getResponse();
return JsonReader.getApiResponse(response, responseType);
}
}
23 changes: 23 additions & 0 deletions api-sdk/src/main/java/com/smartling/api/sdk/JsonReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.smartling.api.sdk;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.smartling.api.sdk.dto.ApiResponse;
import com.smartling.api.sdk.dto.ApiResponseWrapper;
import com.smartling.api.sdk.dto.Data;
import com.smartling.api.sdk.util.DateTypeAdapter;

import java.util.Date;

public class JsonReader {
public static <T extends Data> ApiResponse<T> getApiResponse(String response, TypeToken<ApiResponseWrapper<T>> responseType) {
final GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Date.class, new DateTypeAdapter());

final Gson gson = builder.create();
final ApiResponseWrapper<T> responseWrapper = gson.fromJson(response, responseType.getType());

return responseWrapper.getResponse();
}
}

0 comments on commit cc42d38

Please sign in to comment.