Skip to content

Commit 5ddfc2a

Browse files
committed
Support 1.13 API with BodyWriter<T>
1 parent c724aa0 commit 5ddfc2a

File tree

2 files changed

+18
-25
lines changed

2 files changed

+18
-25
lines changed

gson-adapter/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<groupId>io.avaje</groupId>
1111
<artifactId>avaje-http-client-gson</artifactId>
12-
<version>1.13-SNAPSHOT</version>
12+
<version>1.13</version>
1313

1414
<scm>
1515
<developerConnection>scm:git:git@github.com:avaje/avaje-http-client.git</developerConnection>
@@ -27,7 +27,7 @@
2727
<dependency>
2828
<groupId>io.avaje</groupId>
2929
<artifactId>avaje-http-client</artifactId>
30-
<version>1.12</version>
30+
<version>1.13</version>
3131
<scope>provided</scope>
3232
</dependency>
3333

gson-adapter/src/main/java/io/avaje/http/client/gson/GsonBodyAdapter.java

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@
1010
import io.avaje.http.client.BodyReader;
1111
import io.avaje.http.client.BodyWriter;
1212

13-
import java.io.ByteArrayInputStream;
14-
import java.io.ByteArrayOutputStream;
15-
import java.io.IOException;
16-
import java.io.InputStreamReader;
17-
import java.io.OutputStreamWriter;
13+
import java.io.*;
1814
import java.util.List;
1915
import java.util.concurrent.ConcurrentHashMap;
2016

@@ -38,10 +34,8 @@ public class GsonBodyAdapter implements BodyAdapter {
3834

3935
private final Gson gson;
4036

41-
private final ConcurrentHashMap<Class<?>, BodyWriter> beanWriterCache = new ConcurrentHashMap<>();
42-
37+
private final ConcurrentHashMap<Class<?>, BodyWriter<?>> beanWriterCache = new ConcurrentHashMap<>();
4338
private final ConcurrentHashMap<Class<?>, BodyReader<?>> beanReaderCache = new ConcurrentHashMap<>();
44-
4539
private final ConcurrentHashMap<Class<?>, BodyReader<?>> listReaderCache = new ConcurrentHashMap<>();
4640

4741
/**
@@ -51,11 +45,12 @@ public GsonBodyAdapter(Gson gson) {
5145
this.gson = gson;
5246
}
5347

48+
@SuppressWarnings({"unchecked", "rawtypes"})
5449
@Override
55-
public BodyWriter beanWriter(Class<?> cls) {
56-
return beanWriterCache.computeIfAbsent(cls, aClass -> {
50+
public <T> BodyWriter<T> beanWriter(Class<?> cls) {
51+
return (BodyWriter<T>) beanWriterCache.computeIfAbsent(cls, aClass -> {
5752
try {
58-
final TypeAdapter<?> adapter = gson.getAdapter(cls);
53+
final TypeAdapter adapter = gson.getAdapter(cls);
5954
return new Writer(gson, adapter);
6055
} catch (Exception e) {
6156
throw new RuntimeException(e);
@@ -76,14 +71,14 @@ public <T> BodyReader<T> beanReader(Class<T> cls) {
7671
});
7772
}
7873

79-
@SuppressWarnings("unchecked")
74+
@SuppressWarnings({"unchecked", "rawtypes"})
8075
@Override
8176
public <T> BodyReader<List<T>> listReader(Class<T> cls) {
8277
return (BodyReader<List<T>>) listReaderCache.computeIfAbsent(cls, aClass -> {
8378
try {
84-
final TypeToken<?> listType = TypeToken.getParameterized(List.class, cls);
85-
final TypeAdapter adapter = gson.getAdapter(listType);
86-
return new Reader(gson, adapter);
79+
final TypeToken listType = TypeToken.getParameterized(List.class, cls);
80+
final TypeAdapter<List<T>> adapter = gson.getAdapter(listType);
81+
return new Reader<>(gson, adapter);
8782
} catch (Exception e) {
8883
throw new RuntimeException(e);
8984
}
@@ -114,8 +109,7 @@ public T readBody(String content) {
114109

115110
@Override
116111
public T read(BodyContent body) {
117-
try {
118-
InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(body.content()));
112+
try (InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(body.content()))) {
119113
final JsonReader jsonReader = gson.newJsonReader(reader);
120114
return adapter.read(jsonReader);
121115
} catch (IOException e) {
@@ -124,24 +118,23 @@ public T read(BodyContent body) {
124118
}
125119
}
126120

127-
@SuppressWarnings({"rawtypes"})
128-
private static class Writer implements BodyWriter {
121+
private static class Writer<T> implements BodyWriter<T> {
129122

130123
private final Gson gson;
131-
private final TypeAdapter adapter;
124+
private final TypeAdapter<T> adapter;
132125

133-
Writer(Gson gson, TypeAdapter adapter) {
126+
Writer(Gson gson, TypeAdapter<T> adapter) {
134127
this.gson = gson;
135128
this.adapter = adapter;
136129
}
137130

138131
@Override
139-
public BodyContent write(Object bean, String contentType) {
132+
public BodyContent write(T bean, String contentType) {
140133
return write(bean);
141134
}
142135

143136
@Override
144-
public BodyContent write(Object bean) {
137+
public BodyContent write(T bean) {
145138
try {
146139
ByteArrayOutputStream os = new ByteArrayOutputStream(200);
147140
JsonWriter jsonWriter = gson.newJsonWriter(new OutputStreamWriter(os, UTF_8));

0 commit comments

Comments
 (0)