Skip to content
Permalink
Browse files
deps: switch from Jackson to GSON (#368)
* switch from Jackson to GSON

* format
  • Loading branch information
elharo committed Feb 2, 2021
1 parent 107a32a commit 220df176826fe154e36dadd19702c307cc232c16
@@ -65,7 +65,7 @@
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-jackson2</artifactId>
<artifactId>google-http-client-gson</artifactId>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
@@ -30,8 +30,7 @@
import com.google.api.client.json.GenericJson;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.JsonObjectParser;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.Charsets;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.core.ApiClock;
import com.google.api.core.BetaApi;
import com.google.api.core.CurrentMillisClock;
@@ -61,6 +60,7 @@
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
@@ -516,19 +516,18 @@ protected static String getServiceAccountProjectId() {

@InternalApi("Visible for testing")
static String getValueFromCredentialsFile(String credentialsPath, String key) {
String value = null;
if (credentialsPath != null) {
try (InputStream credentialsStream = new FileInputStream(credentialsPath)) {
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
JsonFactory jsonFactory = GsonFactory.getDefaultInstance();
JsonObjectParser parser = new JsonObjectParser(jsonFactory);
GenericJson fileContents =
parser.parseAndClose(credentialsStream, Charsets.UTF_8, GenericJson.class);
value = (String) fileContents.get(key);
} catch (IOException e) {
// ignore
parser.parseAndClose(credentialsStream, StandardCharsets.UTF_8, GenericJson.class);
return (String) fileContents.get(key);
} catch (IOException | IllegalArgumentException ex) {
return null;
}
}
return value;
return null;
}

/**
@@ -45,6 +45,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
@@ -441,9 +442,11 @@ public void testGetServiceAccountProjectId() throws Exception {
public void testGetServiceAccountProjectId_badJson() throws Exception {
File credentialsFile = File.createTempFile("credentials", ".json");
credentialsFile.deleteOnExit();
Files.write("asdfghj".getBytes(), credentialsFile);
Files.write("asdfghj".getBytes(StandardCharsets.UTF_8), credentialsFile);

assertNull(ServiceOptions.getValueFromCredentialsFile(credentialsFile.getPath(), "project_id"));
String valueFromCredentialsFile =
ServiceOptions.getValueFromCredentialsFile(credentialsFile.getPath(), "project_id");
assertNull(valueFromCredentialsFile);
}

@Test

0 comments on commit 220df17

Please sign in to comment.