Skip to content

Commit 2e14fab

Browse files
authored
Merge pull request #156 from FabianTerhorst/moshi
Replace gson with moshi to improve performance and reduce library size
2 parents 26abd97 + 4ceb627 commit 2e14fab

5 files changed

Lines changed: 23 additions & 24 deletions

File tree

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ checkstyleMain.doLast {
9494
}
9595

9696
dependencies {
97-
compile 'com.google.code.gson:gson:2.7'
97+
compile 'com.squareup.okio:okio:1.9.0'
98+
compile 'com.squareup.moshi:moshi:1.2.0'
9899
compile 'com.annimon:stream:1.1.1'
99100
compile 'com.squareup.okhttp3:okhttp:3.4.0-RC1'
100101
compile 'com.google.protobuf:protobuf-java:3.0.0-beta-3'

src/main/java/com/pokegoapi/auth/GoogleAuthJson.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@
1515

1616
package com.pokegoapi.auth;
1717

18-
import com.google.gson.annotations.SerializedName;
18+
import com.squareup.moshi.Json;
1919

2020
public class GoogleAuthJson {
21-
@SerializedName("device_code")
21+
@Json(name = "device_code")
2222
String deviceCode;
23-
@SerializedName("user_code")
23+
@Json(name = "user_code")
2424
String userCode;
25-
@SerializedName("verification_url")
25+
@Json(name = "verification_url")
2626
String verificationUrl;
27-
@SerializedName("expires_in")
27+
@Json(name = "expires_in")
2828
int expiresIn;
29-
@SerializedName("interval")
29+
@Json(name = "interval")
3030
int interval;
3131

3232
public String getDeviceCode() {

src/main/java/com/pokegoapi/auth/GoogleAuthTokenJson.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@
1515

1616
package com.pokegoapi.auth;
1717

18-
import com.google.gson.annotations.SerializedName;
18+
import com.squareup.moshi.Json;
1919

2020
public class GoogleAuthTokenJson {
2121
private String error;
22-
@SerializedName("access_token")
22+
@Json(name = "access_token")
2323
private String accessToken;
24-
@SerializedName("token_type")
24+
@Json(name = "token_type")
2525
private String tokenType;
26-
@SerializedName("expires_in")
26+
@Json(name = "expires_in")
2727
private int expiresIn;
28-
@SerializedName("refresh_token")
28+
@Json(name = "refresh_token")
2929
private String refreshToken;
30-
@SerializedName("id_token")
30+
@Json(name = "id_token")
3131
private String idToken;
3232

3333
public String getError() {

src/main/java/com/pokegoapi/auth/GoogleLogin.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616
package com.pokegoapi.auth;
1717

1818
import POGOProtos.Networking.Envelopes.RequestEnvelopeOuterClass.RequestEnvelope.AuthInfo;
19-
import com.google.gson.Gson;
20-
import com.google.gson.GsonBuilder;
2119
import com.pokegoapi.exceptions.LoginFailedException;
2220
import com.pokegoapi.util.Log;
21+
import com.squareup.moshi.Moshi;
2322
import okhttp3.HttpUrl;
2423
import okhttp3.OkHttpClient;
2524
import okhttp3.Request;
@@ -80,9 +79,9 @@ public AuthInfo login(String username, String password) throws LoginFailedExcept
8079

8180
Response response = client.newCall(request).execute();
8281

83-
Gson gson = new GsonBuilder().create();
82+
Moshi moshi = new Moshi.Builder().build();
8483

85-
GoogleAuthJson googleAuth = gson.fromJson(response.body().string(), GoogleAuthJson.class);
84+
GoogleAuthJson googleAuth = moshi.adapter(GoogleAuthJson.class).fromJson(response.body().string());
8685
Log.d(TAG, "Get user to go to:"
8786
+ googleAuth.getVerificationUrl()
8887
+ " and enter code:" + googleAuth.getUserCode());
@@ -125,8 +124,8 @@ private GoogleAuthTokenJson poll(GoogleAuthJson json) throws URISyntaxException,
125124

126125
Response response = client.newCall(request).execute();
127126

128-
Gson gson = new GsonBuilder().create();
129-
GoogleAuthTokenJson token = gson.fromJson(response.body().string(), GoogleAuthTokenJson.class);
127+
Moshi moshi = new Moshi.Builder().build();
128+
GoogleAuthTokenJson token = moshi.adapter(GoogleAuthTokenJson.class).fromJson(response.body().string());
130129

131130
if (token.getError() == null) {
132131
return token;

src/main/java/com/pokegoapi/auth/PtcLogin.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616
package com.pokegoapi.auth;
1717

1818
import POGOProtos.Networking.Envelopes.RequestEnvelopeOuterClass.RequestEnvelope.AuthInfo;
19-
import com.google.gson.Gson;
20-
import com.google.gson.GsonBuilder;
2119
import com.pokegoapi.exceptions.LoginFailedException;
20+
import com.squareup.moshi.Moshi;
2221
import lombok.Getter;
2322
import okhttp3.Cookie;
2423
import okhttp3.CookieJar;
@@ -122,9 +121,9 @@ public AuthInfo login(String username, String password) throws LoginFailedExcept
122121

123122
Response getResponse = client.newCall(get).execute();
124123

125-
Gson gson = new GsonBuilder().create();
124+
Moshi moshi = new Moshi.Builder().build();
126125

127-
PtcAuthJson ptcAuth = gson.fromJson(getResponse.body().string(), PtcAuthJson.class);
126+
PtcAuthJson ptcAuth = moshi.adapter(PtcAuthJson.class).fromJson(getResponse.body().string());
128127

129128
HttpUrl url = HttpUrl.parse(LOGIN_URL).newBuilder()
130129
.addQueryParameter("lt", ptcAuth.getLt())
@@ -152,7 +151,7 @@ public AuthInfo login(String username, String password) throws LoginFailedExcept
152151
String body = response.body().string();
153152

154153
if (body.length() > 0) {
155-
PtcError ptcError = gson.fromJson(body, PtcError.class);
154+
PtcError ptcError = moshi.adapter(PtcError.class).fromJson(body);
156155
if (ptcError.getError() != null && ptcError.getError().length() > 0) {
157156
throw new LoginFailedException();
158157
}

0 commit comments

Comments
 (0)