Datasets:
ArXiv:
License:
File size: 991 Bytes
c574d3a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package httpcli.adapter.gson;
import com.google.gson.Gson;
import com.google.gson.JsonIOException;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import httpcli.ResponseBody;
import httpcli.adapter.RespBodyAdapter;
public class RespBodyGson<T> implements RespBodyAdapter<T> {
public final Gson gson;
public final TypeAdapter<T> adapter;
public RespBodyGson(Gson gson, TypeAdapter<T> adapter) {
this.gson = gson;
this.adapter = adapter;
}
@Override
public T parse(ResponseBody respBody) throws Exception {
JsonReader jsonReader = gson.newJsonReader(respBody.charStream());
try {
T result = adapter.read(jsonReader);
if (jsonReader.peek() != JsonToken.END_DOCUMENT) {
throw new JsonIOException("JSON document was not fully consumed.");
}
return result;
} finally {
respBody.close();
}
}
}
|