From 220df176826fe154e36dadd19702c307cc232c16 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Tue, 2 Feb 2021 18:21:35 +0000 Subject: [PATCH] deps: switch from Jackson to GSON (#368) * switch from Jackson to GSON * format --- google-cloud-core/pom.xml | 2 +- .../java/com/google/cloud/ServiceOptions.java | 17 ++++++++--------- .../com/google/cloud/ServiceOptionsTest.java | 7 +++++-- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/google-cloud-core/pom.xml b/google-cloud-core/pom.xml index 176fe2a18b..e297d4633b 100644 --- a/google-cloud-core/pom.xml +++ b/google-cloud-core/pom.xml @@ -65,7 +65,7 @@ com.google.http-client - google-http-client-jackson2 + google-http-client-gson com.google.protobuf diff --git a/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java b/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java index 2a84403af0..7891088d34 100644 --- a/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java +++ b/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java @@ -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; } /** diff --git a/google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java b/google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java index 6f8763c84b..bf75fca161 100644 --- a/google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java +++ b/google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java @@ -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