Replace usage of JSONObject with Moshi

This commit is contained in:
cketti 2022-04-28 23:23:44 +02:00
parent 9cece907d3
commit 906cc19b6e
3 changed files with 19 additions and 7 deletions

View file

@ -11,6 +11,7 @@ dependencies {
implementation "com.squareup.okio:okio:${versions.okio}"
implementation "commons-io:commons-io:${versions.commonsIo}"
implementation "com.jakewharton.timber:timber:${versions.timber}"
implementation "com.squareup.moshi:moshi:${versions.moshi}"
testImplementation project(":mail:testing")
testImplementation "org.robolectric:robolectric:${versions.robolectric}"

View file

@ -1,10 +1,13 @@
package com.fsck.k9.mail.oauth;
import java.io.IOException;
import com.fsck.k9.mail.K9MailLib;
import com.fsck.k9.mail.filter.Base64;
import org.json.JSONException;
import org.json.JSONObject;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.JsonDataException;
import com.squareup.moshi.Moshi;
import timber.log.Timber;
@ -24,13 +27,15 @@ public class XOAuth2ChallengeParser {
}
try {
JSONObject json = new JSONObject(decodedResponse);
String status = json.getString("status");
if (!BAD_RESPONSE.equals(status)) {
Moshi moshi = new Moshi.Builder().build();
JsonAdapter<XOAuth2Response> adapter = moshi.adapter(XOAuth2Response.class);
XOAuth2Response responseObject = adapter.fromJson(decodedResponse);
if (responseObject != null && responseObject.status != null &&
!BAD_RESPONSE.equals(responseObject.status)) {
return false;
}
} catch (JSONException jsonException) {
Timber.e("Error decoding JSON response from: %s. Response was: %s", host, decodedResponse);
} catch (IOException | JsonDataException e) {
Timber.e(e, "Error decoding JSON response from: %s. Response was: %s", host, decodedResponse);
}
return true;

View file

@ -0,0 +1,6 @@
package com.fsck.k9.mail.oauth;
class XOAuth2Response {
public String status;
}