Skip to content
Permalink
Browse files
fix: switch to GSON (#531)
* switch to GSON

* format
  • Loading branch information
elharo committed Dec 30, 2020
1 parent 580cf9e commit 1b98d5c86fc5e56187c977e7f43c39bb62483d40
@@ -72,9 +72,9 @@ public static ClientId of(String clientId, String clientSecret) {
/**
* Constructs a Client ID from JSON from a downloaded file.
*
* @param json The JSON from the downloaded file.
* @return the ClientId instance based on the JSON.
* @throws IOException The JSON could not be parsed.
* @param json the JSON from the downloaded file
* @return the ClientId instance based on the JSON
* @throws IOException the JSON could not be parsed
*/
public static ClientId fromJson(Map<String, Object> json) throws IOException {
Object rawDetail = null;
@@ -105,9 +105,9 @@ public static ClientId fromJson(Map<String, Object> json) throws IOException {
/**
* Constructs a Client ID from JSON file stored as a resource.
*
* @param relativeClass A class in the same namespace as the resource.
* @param resourceName The name of the resource
* @return The constructed ClientID instance based on the JSON in the resource.
* @param relativeClass a class in the same namespace as the resource
* @param resourceName the name of the resource
* @return the constructed ClientID instance based on the JSON in the resource
* @throws IOException The JSON could not be loaded or parsed.
*/
public static ClientId fromResource(Class<?> relativeClass, String resourceName)
@@ -119,9 +119,9 @@ public static ClientId fromResource(Class<?> relativeClass, String resourceName)
/**
* Constructs a Client ID from JSON file stream.
*
* @param stream Stream of the downloaded JSON file.
* @return The constructed ClientID instance based on the JSON in the stream.
* @throws IOException The JSON could not be read or parsed.
* @param stream the downloaded JSON file
* @return the constructed ClientID instance based on the JSON in the stream
* @throws IOException the JSON could not be read or parsed
*/
public static ClientId fromStream(InputStream stream) throws IOException {
Preconditions.checkNotNull(stream);
@@ -37,7 +37,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.json.gson.GsonFactory;
import com.google.auth.http.AuthHttpConstants;
import com.google.auth.http.HttpTransportFactory;
import com.google.common.io.ByteStreams;
@@ -66,7 +66,7 @@ class OAuth2Utils {

static final HttpTransportFactory HTTP_TRANSPORT_FACTORY = new DefaultHttpTransportFactory();

static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();

static final Charset UTF_8 = Charset.forName("UTF-8");

@@ -36,7 +36,7 @@

import com.google.api.client.json.GenericJson;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.auth.http.AuthHttpConstants;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
@@ -54,7 +54,7 @@ public class TestUtils {

public static final String UTF_8 = "UTF-8";

private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();

public static void assertContainsBearerToken(Map<String, List<String>> metadata, String token) {
assertNotNull(metadata);
@@ -33,7 +33,6 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

import com.google.api.client.json.GenericJson;
import com.google.auth.TestUtils;
@@ -156,7 +155,7 @@ public void fromStream() throws IOException {
}

@Test
public void fromStream_invalidJson_throws() {
public void fromStream_invalidJson_doesNotThrow() throws IOException {
String invalidJson =
"{"
+ "\"web\": {"
@@ -169,12 +168,7 @@ public void fromStream_invalidJson_throws() {
+ "}"; // No closing brace
InputStream stream = TestUtils.stringToInputStream(invalidJson);

try {
ClientId.fromStream(stream);
fail();
} catch (IOException expected) {
// Expected
}
ClientId.fromStream(stream);
}

private GenericJson writeClientIdJson(String type, String clientId, String clientSecret) {
@@ -43,7 +43,7 @@
import com.google.api.client.json.GenericJson;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.JsonGenerator;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.json.webtoken.JsonWebToken.Payload;
import com.google.api.client.testing.http.MockLowLevelHttpRequest;
import com.google.api.client.util.Clock;
@@ -111,7 +111,7 @@ public class ImpersonatedCredentialsTest extends BaseSerializationTest {
private static final String ACCESS_TOKEN = "1/MkSJoj1xsli0AccessToken_NKPY2";
private static final int VALID_LIFETIME = 300;
private static final int INVALID_LIFETIME = 43210;
private static JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();

private static final String RFC3339 = "yyyy-MM-dd'T'HH:mm:ss'Z'";

@@ -693,7 +693,7 @@ private String generateErrorJson(
int errorCode, String errorMessage, String errorDomain, String errorReason)
throws IOException {

JsonFactory factory = new JacksonFactory();
JsonFactory factory = new GsonFactory();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
JsonGenerator generator = factory.createJsonGenerator(bout, Charset.defaultCharset());
generator.enablePrettyPrint();
@@ -38,7 +38,7 @@
import static org.junit.Assert.fail;

import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.json.webtoken.JsonWebSignature;
import com.google.api.client.util.Clock;
import com.google.auth.http.AuthHttpConstants;
@@ -69,7 +69,7 @@ public class JwtCredentialsTest extends BaseSerializationTest {
+ "==\n-----END PRIVATE KEY-----\n";
private static final String JWT_ACCESS_PREFIX =
ServiceAccountJwtAccessCredentials.JWT_ACCESS_PREFIX;
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();

static PrivateKey getPrivateKey() {
try {
@@ -36,7 +36,7 @@
import com.google.api.client.json.GenericJson;
import com.google.api.client.json.Json;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.json.webtoken.JsonWebSignature;
import com.google.api.client.testing.http.MockHttpTransport;
import com.google.api.client.testing.http.MockLowLevelHttpRequest;
@@ -54,7 +54,7 @@
public class MockTokenServerTransport extends MockHttpTransport {

static final String EXPECTED_GRANT_TYPE = "urn:ietf:params:oauth:grant-type:jwt-bearer";
static final JsonFactory JSON_FACTORY = new JacksonFactory();
static final JsonFactory JSON_FACTORY = new GsonFactory();
int buildRequestCount;
final Map<String, String> clients = new HashMap<String, String>();
final Map<String, String> refreshTokens = new HashMap<String, String>();
@@ -42,7 +42,7 @@
import static org.junit.Assert.fail;

import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.json.webtoken.JsonWebSignature;
import com.google.api.client.util.Clock;
import com.google.auth.Credentials;
@@ -92,7 +92,7 @@ public class ServiceAccountJwtAccessCredentialsTest extends BaseSerializationTes
private static final String JWT_ACCESS_PREFIX =
ServiceAccountJwtAccessCredentials.JWT_ACCESS_PREFIX;
private static final URI CALL_URI = URI.create("http://googleapis.com/testapi/v1/foo");
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
private static final String QUOTA_PROJECT = "sample-quota-project-id";

@Test
@@ -80,7 +80,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.guava</groupId>

0 comments on commit 1b98d5c

Please sign in to comment.