| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public Object decode(Response response, Type type) throws IOException { |
| 47 | JsonAdapter<Object> jsonAdapter = moshi.adapter(type); |
| 48 | |
| 49 | if (response.status() == 404 || response.status() == 204) return Util.emptyValueOf(type); |
| 50 | if (response.body() == null) return null; |
| 51 | |
| 52 | try (BufferedSource source = Okio.buffer(Okio.source(response.body().asInputStream()))) { |
| 53 | if (source.exhausted()) { |
| 54 | return null; // empty body |
| 55 | } |
| 56 | return jsonAdapter.fromJson(source); |
| 57 | } catch (JsonDataException e) { |
| 58 | if (e.getCause() != null && e.getCause() instanceof IOException) { |
| 59 | throw (IOException) e.getCause(); |
| 60 | } |
| 61 | throw e; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public Object convert(Object object, Type type) throws IOException { |