From ad103f421ea546c1cb929eef0610eac5291a0607 Mon Sep 17 00:00:00 2001 From: jwilson Date: Thu, 21 Apr 2016 23:20:30 -1000 Subject: [PATCH] Use Moshi in OkHttp. As discussed here: https://github.com/square/okhttp/pull/2492 --- okhttp-hpacktests/pom.xml | 12 +++++----- .../HpackDecodeInteropTest.java | 6 ++--- .../{spdy => framed}/HpackDecodeTestBase.java | 8 +++---- .../{spdy => framed}/HpackRoundTripTest.java | 6 ++--- .../{spdy => framed}/hpackjson/Case.java | 4 ++-- .../hpackjson/HpackJsonUtil.java | 22 +++++++++++-------- .../{spdy => framed}/hpackjson/Story.java | 4 ++-- okhttp-tests/pom.xml | 5 ----- pom.xml | 22 +++++++++---------- samples/guide/pom.xml | 4 ++-- ...hGson.java => ParseResponseWithMoshi.java} | 12 +++++----- .../recipes/RequestBodyCompression.java | 9 ++++++-- samples/simple-client/pom.xml | 4 ++-- .../okhttp3/sample/OkHttpContributors.java | 16 ++++++-------- 14 files changed, 68 insertions(+), 66 deletions(-) rename okhttp-hpacktests/src/test/java/okhttp3/internal/{spdy => framed}/HpackDecodeInteropTest.java (87%) rename okhttp-hpacktests/src/test/java/okhttp3/internal/{spdy => framed}/HpackDecodeTestBase.java (93%) rename okhttp-hpacktests/src/test/java/okhttp3/internal/{spdy => framed}/HpackRoundTripTest.java (93%) rename okhttp-hpacktests/src/test/java/okhttp3/internal/{spdy => framed}/hpackjson/Case.java (93%) rename okhttp-hpacktests/src/test/java/okhttp3/internal/{spdy => framed}/hpackjson/HpackJsonUtil.java (80%) rename okhttp-hpacktests/src/test/java/okhttp3/internal/{spdy => framed}/hpackjson/Story.java (94%) rename samples/guide/src/main/java/okhttp3/recipes/{ParseResponseWithGson.java => ParseResponseWithMoshi.java} (80%) diff --git a/okhttp-hpacktests/pom.xml b/okhttp-hpacktests/pom.xml index 0a64ace19789..7e8fb57ad753 100644 --- a/okhttp-hpacktests/pom.xml +++ b/okhttp-hpacktests/pom.xml @@ -8,7 +8,7 @@ com.squareup.okhttp3 parent - 3.0.0-SNAPSHOT + 3.3.0-SNAPSHOT okhttp-hpacktests @@ -19,6 +19,10 @@ com.squareup.okio okio + + com.squareup.moshi + moshi + ${project.groupId} okhttp @@ -41,12 +45,6 @@ ${project.version} test - - - com.google.code.gson - gson - compile - diff --git a/okhttp-hpacktests/src/test/java/okhttp3/internal/spdy/HpackDecodeInteropTest.java b/okhttp-hpacktests/src/test/java/okhttp3/internal/framed/HpackDecodeInteropTest.java similarity index 87% rename from okhttp-hpacktests/src/test/java/okhttp3/internal/spdy/HpackDecodeInteropTest.java rename to okhttp-hpacktests/src/test/java/okhttp3/internal/framed/HpackDecodeInteropTest.java index 74ca6a2fb23c..e99c2a29cd97 100644 --- a/okhttp-hpacktests/src/test/java/okhttp3/internal/spdy/HpackDecodeInteropTest.java +++ b/okhttp-hpacktests/src/test/java/okhttp3/internal/framed/HpackDecodeInteropTest.java @@ -13,15 +13,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package okhttp3.internal.spdy; +package okhttp3.internal.framed; import java.util.Collection; -import okhttp3.internal.spdy.hpackjson.Story; +import okhttp3.internal.framed.hpackjson.Story; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; -import static okhttp3.internal.spdy.hpackjson.HpackJsonUtil.storiesForCurrentDraft; +import static okhttp3.internal.framed.hpackjson.HpackJsonUtil.storiesForCurrentDraft; @RunWith(Parameterized.class) public class HpackDecodeInteropTest extends HpackDecodeTestBase { diff --git a/okhttp-hpacktests/src/test/java/okhttp3/internal/spdy/HpackDecodeTestBase.java b/okhttp-hpacktests/src/test/java/okhttp3/internal/framed/HpackDecodeTestBase.java similarity index 93% rename from okhttp-hpacktests/src/test/java/okhttp3/internal/spdy/HpackDecodeTestBase.java rename to okhttp-hpacktests/src/test/java/okhttp3/internal/framed/HpackDecodeTestBase.java index 77c7ddf1aa1b..a77c4d8a0262 100644 --- a/okhttp-hpacktests/src/test/java/okhttp3/internal/spdy/HpackDecodeTestBase.java +++ b/okhttp-hpacktests/src/test/java/okhttp3/internal/framed/HpackDecodeTestBase.java @@ -13,15 +13,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package okhttp3.internal.spdy; +package okhttp3.internal.framed; import java.util.ArrayList; import java.util.Collection; import java.util.LinkedHashSet; import java.util.List; -import okhttp3.internal.spdy.hpackjson.Case; -import okhttp3.internal.spdy.hpackjson.HpackJsonUtil; -import okhttp3.internal.spdy.hpackjson.Story; +import okhttp3.internal.framed.hpackjson.Case; +import okhttp3.internal.framed.hpackjson.HpackJsonUtil; +import okhttp3.internal.framed.hpackjson.Story; import okio.Buffer; import static org.junit.Assert.assertEquals; diff --git a/okhttp-hpacktests/src/test/java/okhttp3/internal/spdy/HpackRoundTripTest.java b/okhttp-hpacktests/src/test/java/okhttp3/internal/framed/HpackRoundTripTest.java similarity index 93% rename from okhttp-hpacktests/src/test/java/okhttp3/internal/spdy/HpackRoundTripTest.java rename to okhttp-hpacktests/src/test/java/okhttp3/internal/framed/HpackRoundTripTest.java index 22eecd77668e..8405a1fd7f87 100644 --- a/okhttp-hpacktests/src/test/java/okhttp3/internal/spdy/HpackRoundTripTest.java +++ b/okhttp-hpacktests/src/test/java/okhttp3/internal/framed/HpackRoundTripTest.java @@ -13,11 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package okhttp3.internal.spdy; +package okhttp3.internal.framed; import java.util.Collection; -import okhttp3.internal.spdy.hpackjson.Case; -import okhttp3.internal.spdy.hpackjson.Story; +import okhttp3.internal.framed.hpackjson.Case; +import okhttp3.internal.framed.hpackjson.Story; import okio.Buffer; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/okhttp-hpacktests/src/test/java/okhttp3/internal/spdy/hpackjson/Case.java b/okhttp-hpacktests/src/test/java/okhttp3/internal/framed/hpackjson/Case.java similarity index 93% rename from okhttp-hpacktests/src/test/java/okhttp3/internal/spdy/hpackjson/Case.java rename to okhttp-hpacktests/src/test/java/okhttp3/internal/framed/hpackjson/Case.java index 6ff0eb057801..4c931b7c0e6e 100644 --- a/okhttp-hpacktests/src/test/java/okhttp3/internal/spdy/hpackjson/Case.java +++ b/okhttp-hpacktests/src/test/java/okhttp3/internal/framed/hpackjson/Case.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package okhttp3.internal.spdy.hpackjson; +package okhttp3.internal.framed.hpackjson; import java.util.ArrayList; import java.util.LinkedHashMap; @@ -24,7 +24,7 @@ /** * Representation of an individual case (set of headers and wire format). There are many cases for a - * single story. This class is used reflectively with Gson to parse stories. + * single story. This class is used reflectively with Moshi to parse stories. */ public class Case implements Cloneable { diff --git a/okhttp-hpacktests/src/test/java/okhttp3/internal/spdy/hpackjson/HpackJsonUtil.java b/okhttp-hpacktests/src/test/java/okhttp3/internal/framed/hpackjson/HpackJsonUtil.java similarity index 80% rename from okhttp-hpacktests/src/test/java/okhttp3/internal/spdy/hpackjson/HpackJsonUtil.java rename to okhttp-hpacktests/src/test/java/okhttp3/internal/framed/hpackjson/HpackJsonUtil.java index f53ab78859a5..1d38261c5349 100644 --- a/okhttp-hpacktests/src/test/java/okhttp3/internal/spdy/hpackjson/HpackJsonUtil.java +++ b/okhttp-hpacktests/src/test/java/okhttp3/internal/framed/hpackjson/HpackJsonUtil.java @@ -13,19 +13,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package okhttp3.internal.spdy.hpackjson; +package okhttp3.internal.framed.hpackjson; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; +import com.squareup.moshi.JsonAdapter; +import com.squareup.moshi.Moshi; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import okio.Okio; /** * Utilities for reading HPACK tests. @@ -36,20 +35,25 @@ public final class HpackJsonUtil { private static final String STORY_RESOURCE_FORMAT = "/hpack-test-case/%s/story_%02d.json"; - private static final Gson GSON = new GsonBuilder().create(); + private static final Moshi MOSHI = new Moshi.Builder().build(); + private static final JsonAdapter STORY_JSON_ADAPTER = MOSHI.adapter(Story.class); private static Story readStory(InputStream jsonResource) throws IOException { - return GSON.fromJson(new InputStreamReader(jsonResource, "UTF-8"), Story.class); + return STORY_JSON_ADAPTER.fromJson(Okio.buffer(Okio.source(jsonResource))); + } + + private static Story readStory(File file) throws IOException { + return STORY_JSON_ADAPTER.fromJson(Okio.buffer(Okio.source(file))); } /** Iterate through the hpack-test-case resources, only picking stories for the current draft. */ public static String[] storiesForCurrentDraft() throws URISyntaxException { File testCaseDirectory = new File(HpackJsonUtil.class.getResource("/hpack-test-case").toURI()); - List storyNames = new ArrayList(); + List storyNames = new ArrayList<>(); for (File path : testCaseDirectory.listFiles()) { if (path.isDirectory() && Arrays.asList(path.list()).contains("story_00.json")) { try { - Story firstStory = readStory(new FileInputStream(new File(path, "story_00.json"))); + Story firstStory = readStory(new File(path, "story_00.json")); if (firstStory.getDraft() >= BASE_DRAFT) { storyNames.add(path.getName()); } diff --git a/okhttp-hpacktests/src/test/java/okhttp3/internal/spdy/hpackjson/Story.java b/okhttp-hpacktests/src/test/java/okhttp3/internal/framed/hpackjson/Story.java similarity index 94% rename from okhttp-hpacktests/src/test/java/okhttp3/internal/spdy/hpackjson/Story.java rename to okhttp-hpacktests/src/test/java/okhttp3/internal/framed/hpackjson/Story.java index 417d426c7beb..3d14d5dab5f9 100644 --- a/okhttp-hpacktests/src/test/java/okhttp3/internal/spdy/hpackjson/Story.java +++ b/okhttp-hpacktests/src/test/java/okhttp3/internal/framed/hpackjson/Story.java @@ -13,14 +13,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package okhttp3.internal.spdy.hpackjson; +package okhttp3.internal.framed.hpackjson; import java.util.ArrayList; import java.util.List; /** * Representation of one story, a set of request headers to encode or decode. This class is used - * reflectively with Gson to parse stories from files. + * reflectively with Moshi to parse stories from files. */ public class Story implements Cloneable { diff --git a/okhttp-tests/pom.xml b/okhttp-tests/pom.xml index d9f85969224d..137a07931013 100644 --- a/okhttp-tests/pom.xml +++ b/okhttp-tests/pom.xml @@ -45,11 +45,6 @@ ${project.version} test - - com.google.code.gson - gson - test - diff --git a/pom.xml b/pom.xml index 52c283ed1365..0402e289a18d 100644 --- a/pom.xml +++ b/pom.xml @@ -43,18 +43,18 @@ UTF-8 - 1.7 - 1.6.0 + 0.7 7.1.2.v20141202 8.1.2.v20141202 - 1.50 - 2.2.3 + 4.1.1.4 4.2.2 - 0.7 + 1.50 16.0 - 4.1.1.4 + 1.7 + 1.1.0 + 1.6.0 4.11 @@ -96,11 +96,6 @@ bcprov-jdk15on ${bouncycastle.version} - - com.google.code.gson - gson - ${gson.version} - org.apache.httpcomponents httpclient @@ -121,6 +116,11 @@ android ${android.version} + + com.squareup.moshi + moshi + ${moshi.version} + diff --git a/samples/guide/pom.xml b/samples/guide/pom.xml index cb497502f255..d6ccee43e1e6 100644 --- a/samples/guide/pom.xml +++ b/samples/guide/pom.xml @@ -24,8 +24,8 @@ ${project.version} - com.google.code.gson - gson + com.squareup.moshi + moshi diff --git a/samples/guide/src/main/java/okhttp3/recipes/ParseResponseWithGson.java b/samples/guide/src/main/java/okhttp3/recipes/ParseResponseWithMoshi.java similarity index 80% rename from samples/guide/src/main/java/okhttp3/recipes/ParseResponseWithGson.java rename to samples/guide/src/main/java/okhttp3/recipes/ParseResponseWithMoshi.java index 7ca4ccb731e6..80585c10fe6a 100644 --- a/samples/guide/src/main/java/okhttp3/recipes/ParseResponseWithGson.java +++ b/samples/guide/src/main/java/okhttp3/recipes/ParseResponseWithMoshi.java @@ -15,16 +15,18 @@ */ package okhttp3.recipes; -import com.google.gson.Gson; +import com.squareup.moshi.JsonAdapter; +import com.squareup.moshi.Moshi; import java.io.IOException; import java.util.Map; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; -public final class ParseResponseWithGson { +public final class ParseResponseWithMoshi { private final OkHttpClient client = new OkHttpClient(); - private final Gson gson = new Gson(); + private final Moshi moshi = new Moshi.Builder().build(); + private final JsonAdapter gistJsonAdapter = moshi.adapter(Gist.class); public void run() throws Exception { Request request = new Request.Builder() @@ -33,7 +35,7 @@ public void run() throws Exception { Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); - Gist gist = gson.fromJson(response.body().charStream(), Gist.class); + Gist gist = gistJsonAdapter.fromJson(response.body().source()); response.body().close(); for (Map.Entry entry : gist.files.entrySet()) { @@ -51,6 +53,6 @@ static class GistFile { } public static void main(String... args) throws Exception { - new ParseResponseWithGson().run(); + new ParseResponseWithMoshi().run(); } } diff --git a/samples/guide/src/main/java/okhttp3/recipes/RequestBodyCompression.java b/samples/guide/src/main/java/okhttp3/recipes/RequestBodyCompression.java index d1dc18a3b1b3..1dccacc8fac7 100644 --- a/samples/guide/src/main/java/okhttp3/recipes/RequestBodyCompression.java +++ b/samples/guide/src/main/java/okhttp3/recipes/RequestBodyCompression.java @@ -15,7 +15,9 @@ */ package okhttp3.recipes; -import com.google.gson.Gson; +import com.squareup.moshi.JsonAdapter; +import com.squareup.moshi.Moshi; +import com.squareup.moshi.Types; import java.io.IOException; import java.util.LinkedHashMap; import java.util.Map; @@ -42,12 +44,15 @@ public final class RequestBodyCompression { private final OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(new GzipRequestInterceptor()) .build(); + private final Moshi moshi = new Moshi.Builder().build(); + private final JsonAdapter> mapJsonAdapter = moshi.adapter( + Types.newParameterizedType(Map.class, String.class, String.class)); public void run() throws Exception { Map requestBody = new LinkedHashMap<>(); requestBody.put("longUrl", "https://publicobject.com/2014/12/04/html-formatting-javadocs/"); RequestBody jsonRequestBody = RequestBody.create( - MEDIA_TYPE_JSON, new Gson().toJson(requestBody)); + MEDIA_TYPE_JSON, mapJsonAdapter.toJson(requestBody)); Request request = new Request.Builder() .url("https://www.googleapis.com/urlshortener/v1/url?key=" + GOOGLE_API_KEY) .post(jsonRequestBody) diff --git a/samples/simple-client/pom.xml b/samples/simple-client/pom.xml index 1e40e2ea19f5..1327dea146dd 100644 --- a/samples/simple-client/pom.xml +++ b/samples/simple-client/pom.xml @@ -20,8 +20,8 @@ - com.google.code.gson - gson + com.squareup.moshi + moshi diff --git a/samples/simple-client/src/main/java/okhttp3/sample/OkHttpContributors.java b/samples/simple-client/src/main/java/okhttp3/sample/OkHttpContributors.java index bbd5755dec82..ec9019110b92 100644 --- a/samples/simple-client/src/main/java/okhttp3/sample/OkHttpContributors.java +++ b/samples/simple-client/src/main/java/okhttp3/sample/OkHttpContributors.java @@ -1,8 +1,8 @@ package okhttp3.sample; -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; -import java.io.Reader; +import com.squareup.moshi.JsonAdapter; +import com.squareup.moshi.Moshi; +import com.squareup.moshi.Types; import java.util.Collections; import java.util.Comparator; import java.util.List; @@ -13,10 +13,9 @@ public class OkHttpContributors { private static final String ENDPOINT = "https://api.github.com/repos/square/okhttp/contributors"; - private static final Gson GSON = new Gson(); - private static final TypeToken> CONTRIBUTORS = - new TypeToken>() { - }; + private static final Moshi MOSHI = new Moshi.Builder().build(); + private static final JsonAdapter> CONTRIBUTORS_JSON_ADAPTER = MOSHI.adapter( + Types.newParameterizedType(List.class, Contributor.class)); static class Contributor { String login; @@ -36,8 +35,7 @@ public static void main(String... args) throws Exception { // Deserialize HTTP response to concrete type. ResponseBody body = response.body(); - Reader charStream = body.charStream(); - List contributors = GSON.fromJson(charStream, CONTRIBUTORS.getType()); + List contributors = CONTRIBUTORS_JSON_ADAPTER.fromJson(body.source()); body.close(); // Sort list by the most contributions.