(Response response, Type type)
| 56 | public class JsonDecoder implements Decoder, feign.codec.JsonDecoder { |
| 57 | |
| 58 | @Override |
| 59 | public Object decode(Response response, Type type) throws IOException, DecodeException { |
| 60 | if (response.status() == 404 || response.status() == 204) |
| 61 | if (Map.class.equals(type)) return null; |
| 62 | else if (JSONObject.class.isAssignableFrom((Class<?>) type)) return new JSONObject(); |
| 63 | else if (JSONArray.class.isAssignableFrom((Class<?>) type)) return new JSONArray(); |
| 64 | else if (String.class.equals(type)) return null; |
| 65 | else |
| 66 | throw new DecodeException( |
| 67 | response.status(), |
| 68 | format("%s is not a type supported by this decoder.", type), |
| 69 | response.request()); |
| 70 | if (response.body() == null) return null; |
| 71 | try (Reader reader = response.body().asReader(response.charset())) { |
| 72 | Reader bodyReader = (reader.markSupported()) ? reader : new BufferedReader(reader); |
| 73 | bodyReader.mark(1); |
| 74 | if (bodyReader.read() == -1) { |
| 75 | return null; // Empty body |
| 76 | } |
| 77 | bodyReader.reset(); |
| 78 | return decodeBody(response, type, bodyReader); |
| 79 | } catch (JSONException jsonException) { |
| 80 | if (jsonException.getCause() != null && jsonException.getCause() instanceof IOException) { |
| 81 | throw (IOException) jsonException.getCause(); |
| 82 | } |
| 83 | throw new DecodeException( |
| 84 | response.status(), jsonException.getMessage(), response.request(), jsonException); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | private Object decodeBody(Response response, Type type, Reader reader) throws IOException { |
| 89 | if (String.class.equals(type)) return Util.toString(reader); |
nothing calls this directly
no test coverage detected