Skip to content

Commit

Permalink
Move converter into the main package, remove Gson from core.
Browse files Browse the repository at this point in the history
This change means that only RequestBody and ResponseBody are the types which are allowed for their respective objects by default. If you want serialization of any kind, you need to opt-in with either your own converter or one of the provided ones.
  • Loading branch information
JakeWharton committed Jun 3, 2015
1 parent 5777029 commit 9b203b6
Show file tree
Hide file tree
Showing 33 changed files with 328 additions and 204 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package retrofit;

import com.google.gson.reflect.TypeToken;
import com.google.common.reflect.TypeToken;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.ResponseBody;
Expand All @@ -27,7 +27,6 @@
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import retrofit.converter.Converter;
import retrofit.http.GET;
import rx.Observable;
import rx.observables.BlockingObservable;
Expand Down Expand Up @@ -202,7 +201,7 @@ static class StringConverter implements Converter {
}

@Override public RequestBody toBody(Object object, Type type) {
return RequestBody.create(MediaType.parse("text/plain"), String.valueOf(type));
return RequestBody.create(MediaType.parse("text/plain"), String.valueOf(object));
}
}
}
10 changes: 10 additions & 0 deletions retrofit-converters/gson/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Gson Converter
==============

A `Converter` which uses [Gson][1] for serialization to and from JSON.

A default `Gson` instance will be created or one can be configured and passed to the
`GsonConverter` construction to further control the serialization.


[1]: https://github.com/google/gson
43 changes: 43 additions & 0 deletions retrofit-converters/gson/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.squareup.retrofit</groupId>
<artifactId>retrofit-converters</artifactId>
<version>2.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>converter-gson</artifactId>
<name>Converter: Gson</name>

<dependencies>
<dependency>
<groupId>com.squareup.retrofit</groupId>
<artifactId>retrofit</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
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 retrofit.converter;
package retrofit;

import com.google.gson.Gson;
import com.squareup.okhttp.MediaType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2015 Square, Inc.
package retrofit.converter;
package retrofit;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package retrofit.converter;
package retrofit;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package retrofit.converter;
package retrofit;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonMappingException;
Expand Down
1 change: 1 addition & 0 deletions retrofit-converters/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<packaging>pom</packaging>

<modules>
<module>gson</module>
<module>protobuf</module>
<module>jackson</module>
<module>wire</module>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2013 Square, Inc.
package retrofit.converter;
package retrofit;

import com.google.protobuf.AbstractMessageLite;
import com.squareup.okhttp.MediaType;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2013 Square, Inc.
package retrofit.converter;
package retrofit;

import com.google.protobuf.InvalidProtocolBufferException;
import com.squareup.okhttp.MediaType;
Expand All @@ -14,7 +14,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
import static retrofit.converter.PhoneProtos.Phone;
import static retrofit.PhoneProtos.Phone;

public final class ProtoConverterTest {
private static final Phone PROTO = Phone.newBuilder().setNumber("(519) 867-5309").build();
Expand Down
2 changes: 1 addition & 1 deletion retrofit-converters/protobuf/src/test/protos/phone.proto
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package retrofit;

option java_package = "retrofit.converter";
option java_package = "retrofit";
option java_outer_classname = "PhoneProtos";

message Phone {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package retrofit.converter;
package retrofit;

import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.RequestBody;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package retrofit.converter;
package retrofit;

import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.RequestBody;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2013 Square, Inc.
package retrofit.converter;
package retrofit;

import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.RequestBody;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Code generated by Wire protocol buffer compiler, do not edit.
// Source file: ../wire-runtime/src/test/proto/person.proto
package retrofit.converter;
package retrofit;

import com.squareup.wire.Message;
import com.squareup.wire.ProtoEnum;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2013 Square, Inc.
package retrofit.converter;
package retrofit;

import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.RequestBody;
Expand Down
Loading

0 comments on commit 9b203b6

Please sign in to comment.