Skip to content

Commit

Permalink
Merge pull request Grover-c13#156 from FabianTerhorst/moshi
Browse files Browse the repository at this point in the history
Replace gson with moshi to improve performance and reduce library size
  • Loading branch information
Grover-c13 authored Jul 23, 2016
2 parents 26abd97 + 4ceb627 commit 2e14fab
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 24 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ checkstyleMain.doLast {
}

dependencies {
compile 'com.google.code.gson:gson:2.7'
compile 'com.squareup.okio:okio:1.9.0'
compile 'com.squareup.moshi:moshi:1.2.0'
compile 'com.annimon:stream:1.1.1'
compile 'com.squareup.okhttp3:okhttp:3.4.0-RC1'
compile 'com.google.protobuf:protobuf-java:3.0.0-beta-3'
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/pokegoapi/auth/GoogleAuthJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@

package com.pokegoapi.auth;

import com.google.gson.annotations.SerializedName;
import com.squareup.moshi.Json;

public class GoogleAuthJson {
@SerializedName("device_code")
@Json(name = "device_code")
String deviceCode;
@SerializedName("user_code")
@Json(name = "user_code")
String userCode;
@SerializedName("verification_url")
@Json(name = "verification_url")
String verificationUrl;
@SerializedName("expires_in")
@Json(name = "expires_in")
int expiresIn;
@SerializedName("interval")
@Json(name = "interval")
int interval;

public String getDeviceCode() {
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/pokegoapi/auth/GoogleAuthTokenJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@

package com.pokegoapi.auth;

import com.google.gson.annotations.SerializedName;
import com.squareup.moshi.Json;

public class GoogleAuthTokenJson {
private String error;
@SerializedName("access_token")
@Json(name = "access_token")
private String accessToken;
@SerializedName("token_type")
@Json(name = "token_type")
private String tokenType;
@SerializedName("expires_in")
@Json(name = "expires_in")
private int expiresIn;
@SerializedName("refresh_token")
@Json(name = "refresh_token")
private String refreshToken;
@SerializedName("id_token")
@Json(name = "id_token")
private String idToken;

public String getError() {
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/com/pokegoapi/auth/GoogleLogin.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
package com.pokegoapi.auth;

import POGOProtos.Networking.Envelopes.RequestEnvelopeOuterClass.RequestEnvelope.AuthInfo;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.pokegoapi.exceptions.LoginFailedException;
import com.pokegoapi.util.Log;
import com.squareup.moshi.Moshi;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
Expand Down Expand Up @@ -80,9 +79,9 @@ public AuthInfo login(String username, String password) throws LoginFailedExcept

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

Gson gson = new GsonBuilder().create();
Moshi moshi = new Moshi.Builder().build();

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

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

Gson gson = new GsonBuilder().create();
GoogleAuthTokenJson token = gson.fromJson(response.body().string(), GoogleAuthTokenJson.class);
Moshi moshi = new Moshi.Builder().build();
GoogleAuthTokenJson token = moshi.adapter(GoogleAuthTokenJson.class).fromJson(response.body().string());

if (token.getError() == null) {
return token;
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/com/pokegoapi/auth/PtcLogin.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
package com.pokegoapi.auth;

import POGOProtos.Networking.Envelopes.RequestEnvelopeOuterClass.RequestEnvelope.AuthInfo;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.pokegoapi.exceptions.LoginFailedException;
import com.squareup.moshi.Moshi;
import lombok.Getter;
import okhttp3.Cookie;
import okhttp3.CookieJar;
Expand Down Expand Up @@ -122,9 +121,9 @@ public AuthInfo login(String username, String password) throws LoginFailedExcept

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

Gson gson = new GsonBuilder().create();
Moshi moshi = new Moshi.Builder().build();

PtcAuthJson ptcAuth = gson.fromJson(getResponse.body().string(), PtcAuthJson.class);
PtcAuthJson ptcAuth = moshi.adapter(PtcAuthJson.class).fromJson(getResponse.body().string());

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

if (body.length() > 0) {
PtcError ptcError = gson.fromJson(body, PtcError.class);
PtcError ptcError = moshi.adapter(PtcError.class).fromJson(body);
if (ptcError.getError() != null && ptcError.getError().length() > 0) {
throw new LoginFailedException();
}
Expand Down

0 comments on commit 2e14fab

Please sign in to comment.