mirror of
https://github.com/square/okhttp.git
synced 2026-01-12 10:23:16 +03:00
Merge pull request #2502 from square/jwilson_0421_moshi
Use Moshi in OkHttp.
This commit is contained in:
@@ -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>
|
||||
@@ -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>
|
||||
@@ -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>
|
||||
|
||||
@@ -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 {
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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 @@ import okio.ByteString;
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
|
||||
@@ -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> 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());
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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>
|
||||
|
||||
24
pom.xml
24
pom.xml
@@ -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>
|
||||
<apache.http.version>4.2.2</apache.http.version>
|
||||
<airlift.version>0.7</airlift.version>
|
||||
<guava.version>16.0</guava.version>
|
||||
<android.version>4.1.1.4</android.version>
|
||||
<apache.http.version>4.2.2</apache.http.version>
|
||||
<bouncycastle.version>1.50</bouncycastle.version>
|
||||
<guava.version>16.0</guava.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>
|
||||
@@ -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>
|
||||
@@ -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>
|
||||
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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()
|
||||
@@ -33,7 +35,7 @@ public final class ParseResponseWithGson {
|
||||
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()) {
|
||||
@@ -51,6 +53,6 @@ public final class ParseResponseWithGson {
|
||||
}
|
||||
|
||||
public static void main(String... args) throws Exception {
|
||||
new ParseResponseWithGson().run();
|
||||
new ParseResponseWithMoshi().run();
|
||||
}
|
||||
}
|
||||
@@ -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<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)
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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 @@ import okhttp3.ResponseBody;
|
||||
|
||||
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;
|
||||
@@ -36,8 +35,7 @@ public class OkHttpContributors {
|
||||
|
||||
// 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.
|
||||
|
||||
Reference in New Issue
Block a user