Skip to content

Commit

Permalink
Merge pull request square#1603 from square/jw/gson-lenient-reading
Browse files Browse the repository at this point in the history
Use Gson instance as factory for reader to honor config.
  • Loading branch information
swankjesse committed Feb 16, 2016
2 parents d80d65c + 473fd24 commit 2780ca4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<rxjava.version>1.1.1</rxjava.version>

<!-- Converter Dependencies -->
<gson.version>2.4</gson.version>
<gson.version>2.6.1</gson.version>
<protobuf.version>2.5.0</protobuf.version>
<jackson.version>2.4.3</jackson.version>
<wire.version>2.1.0</wire.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private GsonConverterFactory(Gson gson) {
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
Retrofit retrofit) {
TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
return new GsonResponseBodyConverter<>(adapter);
return new GsonResponseBodyConverter<>(gson, adapter);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,26 @@
*/
package retrofit2.converter.gson;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import java.io.IOException;
import okhttp3.ResponseBody;
import retrofit2.Converter;

final class GsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
private final Gson gson;
private final TypeAdapter<T> adapter;

GsonResponseBodyConverter(TypeAdapter<T> adapter) {
GsonResponseBodyConverter(Gson gson, TypeAdapter<T> adapter) {
this.gson = gson;
this.adapter = adapter;
}

@Override public T convert(ResponseBody value) throws IOException {
JsonReader jsonReader = gson.newJsonReader(value.charStream());
try {
return adapter.fromJson(value.charStream());
return adapter.read(jsonReader);
} finally {
value.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Body;
import retrofit2.http.POST;

Expand Down Expand Up @@ -90,6 +89,7 @@ interface Service {
@Before public void setUp() {
Gson gson = new GsonBuilder()
.registerTypeAdapter(AnInterface.class, new AnInterfaceAdapter())
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(server.url("/"))
Expand Down Expand Up @@ -133,4 +133,12 @@ interface Service {
assertThat(request.getBody().readUtf8()).isEqualTo("{}"); // Null value was not serialized.
assertThat(request.getHeader("Content-Type")).isEqualTo("application/json; charset=UTF-8");
}

@Test public void deserializeUsesConfiguration() throws IOException, InterruptedException {
server.enqueue(new MockResponse().setBody("{/* a comment! */}"));

Response<AnImplementation> response =
service.anImplementation(new AnImplementation("value")).execute();
assertThat(response.body().getName()).isNull();
}
}

0 comments on commit 2780ca4

Please sign in to comment.