Skip to content

Commit

Permalink
feat(java): convert from gson to jackson (#771)
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp authored Jul 7, 2022
1 parent d920500 commit cbdfd38
Show file tree
Hide file tree
Showing 20 changed files with 258 additions and 762 deletions.
2 changes: 1 addition & 1 deletion .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ runs:
- name: Download Java formatter
if: inputs.type != 'minimal'
shell: bash
run: curl -L "https://github.com/google/google-java-format/releases/download/v1.13.0/google-java-format-1.13.0-all-deps.jar" > /tmp/java-formatter.jar
run: curl --retry 3 -L "https://github.com/google/google-java-format/releases/download/v1.13.0/google-java-format-1.13.0-all-deps.jar" > /tmp/java-formatter.jar

# JavaScript for monorepo and tooling
- name: Install Node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ dependencies {
implementation 'com.google.code.findbugs:jsr305:3.0.2'
api 'com.squareup.okhttp3:okhttp:4.10.0'
implementation 'com.squareup.okhttp3:logging-interceptor:4.10.0'
api 'com.google.code.gson:gson:2.9.0'
implementation 'io.gsonfire:gson-fire:1.8.5'
implementation 'com.fasterxml.jackson.core:jackson-core:2.13.3'
api 'com.fasterxml.jackson.core:jackson-annotations:2.13.3'
api 'com.fasterxml.jackson.core:jackson-databind:2.13.3'
implementation 'org.openapitools:jackson-databind-nullable:0.2.1'
}

tasks.withType(JavaCompile) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import com.algolia.exceptions.*;
import com.algolia.utils.*;
import com.algolia.utils.retry.StatefulHost;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.type.TypeReference;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.time.OffsetDateTime;
Expand All @@ -24,6 +26,7 @@ public abstract class ApiClient {
private String contentType;

private Requester requester;
private ObjectMapper json;

public ApiClient(String appId, String apiKey, String clientName, String version, ClientOptions options) {
if (appId == null || appId.length() == 0) {
Expand Down Expand Up @@ -54,6 +57,8 @@ public ApiClient(String appId, String apiKey, String clientName, String version,
} else {
this.requester = new HttpRequester();
}

this.json = new JSONBuilder().build();
}

private void refreshUserAgent() {
Expand Down Expand Up @@ -189,13 +194,13 @@ public ApiClient setHosts(List<StatefulHost> hosts) {
* @param param Parameter
* @return String representation of the parameter
*/
public String parameterToString(Object param) {
public String parameterToString(Object param) throws UnsupportedOperationException {
if (param == null) {
return "";
} else if (param instanceof Date || param instanceof OffsetDateTime || param instanceof LocalDate) {
// Serialize to json string and remove the " enclosing characters
String jsonStr = JSON.serialize(param);
return jsonStr.substring(1, jsonStr.length() - 1);
// note: date comes as string for now, we should never have to serialize one
// maybe we could accept them as Date object and in that case use jackson serialization
throw new UnsupportedOperationException("Date must come as string (already serialized)");
} else if (param instanceof Collection) {
StringJoiner b = new StringJoiner(",");
for (Object o : (Collection) param) {
Expand Down Expand Up @@ -233,7 +238,11 @@ public RequestBody serialize(Object obj) throws AlgoliaRuntimeException {
String content;

if (obj != null) {
content = JSON.serialize(obj);
try {
content = json.writeValueAsString(obj);
} catch (JsonProcessingException e) {
throw new AlgoliaRuntimeException(e);
}
} else {
content = null;
}
Expand All @@ -246,9 +255,9 @@ public RequestBody serialize(Object obj) throws AlgoliaRuntimeException {
*
* @param <T> Type
* @param returnType Return type
* @see #execute(Call, Type)
* @see #execute(Call, TypeReference)
*/
public <T> CompletableFuture<T> executeAsync(Call call, final Type returnType) {
public <T> CompletableFuture<T> executeAsync(Call call, final TypeReference returnType) {
final CompletableFuture<T> future = new CompletableFuture<>();
call.enqueue(
new Callback() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import com.algolia.exceptions.*;
import com.algolia.utils.retry.RetryStrategy;
import com.algolia.utils.retry.StatefulHost;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
Expand All @@ -20,6 +22,7 @@ public class HttpRequester implements Requester {
private OkHttpClient httpClient;
private HttpLoggingInterceptor loggingInterceptor;
private LogLevel level;
private ObjectMapper json;

public HttpRequester() {
this.retryStrategy = new RetryStrategy();
Expand All @@ -34,6 +37,8 @@ public HttpRequester() {
builder.retryOnConnectionFailure(false);

httpClient = builder.build();

this.json = new JSONBuilder().build();
}

@Override
Expand All @@ -42,7 +47,7 @@ public Call newCall(Request request) {
}

@Override
public <T> T handleResponse(Response response, Type returnType) throws AlgoliaRuntimeException {
public <T> T handleResponse(Response response, TypeReference returnType) throws AlgoliaRuntimeException {
if (response.isSuccessful()) {
if (returnType == null || response.code() == 204) {
// returning null if the returnType is not defined, or the status code is 204 (No Content)
Expand All @@ -69,12 +74,12 @@ public <T> T handleResponse(Response response, Type returnType) throws AlgoliaRu
}
}

private <T> T deserialize(Response response, Type returnType) throws AlgoliaRuntimeException {
private <T> T deserialize(Response response, TypeReference returnType) throws AlgoliaRuntimeException {
if (response == null || returnType == null) {
return null;
}

if ("byte[]".equals(returnType.toString())) {
if ("byte[]".equals(returnType.getType().getTypeName())) {
// Handle binary response (byte array).
try {
return (T) response.body().bytes();
Expand All @@ -98,8 +103,11 @@ private <T> T deserialize(Response response, Type returnType) throws AlgoliaRunt
if (contentType == null) {
contentType = "application/json";
}

return JSON.deserialize(respBody, returnType);
try {
return (T) json.readValue(respBody, returnType);
} catch (JsonProcessingException e) {
throw new AlgoliaRuntimeException(e);
}
}

@Override
Expand Down
Loading

0 comments on commit cbdfd38

Please sign in to comment.