Skip to content

Commit

Permalink
Use Moshi in OkHttp.
Browse files Browse the repository at this point in the history
As discussed here: square#2492
  • Loading branch information
squarejesse committed Apr 22, 2016
1 parent c9ad163 commit ad103f4
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 66 deletions.
12 changes: 5 additions & 7 deletions okhttp-hpacktests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>parent</artifactId>
<version>3.0.0-SNAPSHOT</version>
<version>3.3.0-SNAPSHOT</version>
</parent>

<artifactId>okhttp-hpacktests</artifactId>
Expand All @@ -19,6 +19,10 @@
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
</dependency>
<dependency>
<groupId>com.squareup.moshi</groupId>
<artifactId>moshi</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>okhttp</artifactId>
Expand All @@ -41,12 +45,6 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<!-- Gson: Java to Json conversion -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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> 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<String> storyNames = new ArrayList<String>();
List<String> 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
5 changes: 0 additions & 5 deletions okhttp-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
22 changes: 11 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,18 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<!-- Compilation -->
<java.version>1.7</java.version>
<okio.version>1.6.0</okio.version>
<airlift.version>0.7</airlift.version>
<!-- ALPN library targeted to Java 7 -->
<alpn.jdk7.version>7.1.2.v20141202</alpn.jdk7.version>
<!-- ALPN library targeted to Java 8 update 25. -->
<alpn.jdk8.version>8.1.2.v20141202</alpn.jdk8.version>
<bouncycastle.version>1.50</bouncycastle.version>
<gson.version>2.2.3</gson.version>
<android.version>4.1.1.4</android.version>
<apache.http.version>4.2.2</apache.http.version>
<airlift.version>0.7</airlift.version>
<bouncycastle.version>1.50</bouncycastle.version>
<guava.version>16.0</guava.version>
<android.version>4.1.1.4</android.version>
<java.version>1.7</java.version>
<moshi.version>1.1.0</moshi.version>
<okio.version>1.6.0</okio.version>

<!-- Test Dependencies -->
<junit.version>4.11</junit.version>
Expand Down Expand Up @@ -96,11 +96,6 @@
<artifactId>bcprov-jdk15on</artifactId>
<version>${bouncycastle.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
Expand All @@ -121,6 +116,11 @@
<artifactId>android</artifactId>
<version>${android.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.moshi</groupId>
<artifactId>moshi</artifactId>
<version>${moshi.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
4 changes: 2 additions & 2 deletions samples/guide/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<groupId>com.squareup.moshi</groupId>
<artifactId>moshi</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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<Gist> gistJsonAdapter = moshi.adapter(Gist.class);

public void run() throws Exception {
Request request = new Request.Builder()
Expand All @@ -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<String, GistFile> entry : gist.files.entrySet()) {
Expand All @@ -51,6 +53,6 @@ static class GistFile {
}

public static void main(String... args) throws Exception {
new ParseResponseWithGson().run();
new ParseResponseWithMoshi().run();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Map<String, String>> mapJsonAdapter = moshi.adapter(
Types.newParameterizedType(Map.class, String.class, String.class));

public void run() throws Exception {
Map<String, String> 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)
Expand Down
4 changes: 2 additions & 2 deletions samples/simple-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<groupId>com.squareup.moshi</groupId>
<artifactId>moshi</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<List<Contributor>> CONTRIBUTORS =
new TypeToken<List<Contributor>>() {
};
private static final Moshi MOSHI = new Moshi.Builder().build();
private static final JsonAdapter<List<Contributor>> CONTRIBUTORS_JSON_ADAPTER = MOSHI.adapter(
Types.newParameterizedType(List.class, Contributor.class));

static class Contributor {
String login;
Expand All @@ -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<Contributor> contributors = GSON.fromJson(charStream, CONTRIBUTORS.getType());
List<Contributor> contributors = CONTRIBUTORS_JSON_ADAPTER.fromJson(body.source());
body.close();

// Sort list by the most contributions.
Expand Down

0 comments on commit ad103f4

Please sign in to comment.