diff --git a/json/src/main/java/feign/json/JsonDecoder.java b/json/src/main/java/feign/json/JsonDecoder.java index 597d6b5568..13786cd3f2 100644 --- a/json/src/main/java/feign/json/JsonDecoder.java +++ b/json/src/main/java/feign/json/JsonDecoder.java @@ -14,6 +14,7 @@ package feign.json; import feign.Response; +import feign.Util; import feign.codec.DecodeException; import feign.codec.Decoder; import org.json.JSONArray; @@ -57,6 +58,8 @@ public Object decode(Response response, Type type) throws IOException, DecodeExc return new JSONObject(); else if (JSONArray.class.isAssignableFrom((Class) type)) return new JSONArray(); + else if (String.class.equals(type)) + return null; else throw new DecodeException(response.status(), format("%s is not a type supported by this decoder.", type), response.request()); @@ -69,7 +72,7 @@ else if (JSONArray.class.isAssignableFrom((Class) type)) return null; // Empty body } bodyReader.reset(); - return decodeJSON(response, type, bodyReader); + return decodeBody(response, type, bodyReader); } catch (JSONException jsonException) { if (jsonException.getCause() != null && jsonException.getCause() instanceof IOException) { throw (IOException) jsonException.getCause(); @@ -79,7 +82,9 @@ else if (JSONArray.class.isAssignableFrom((Class) type)) } } - private Object decodeJSON(Response response, Type type, Reader reader) { + private Object decodeBody(Response response, Type type, Reader reader) throws IOException { + if (String.class.equals(type)) + return Util.toString(reader); JSONTokener tokenizer = new JSONTokener(reader); if (JSONObject.class.isAssignableFrom((Class) type)) return new JSONObject(tokenizer); diff --git a/json/src/test/java/feign/json/JsonDecoderTest.java b/json/src/test/java/feign/json/JsonDecoderTest.java index 410ec827bd..0d9c5877c5 100644 --- a/json/src/test/java/feign/json/JsonDecoderTest.java +++ b/json/src/test/java/feign/json/JsonDecoderTest.java @@ -78,6 +78,19 @@ public void decodesObject() throws IOException { assertTrue(jsonObject.similar(new JsonDecoder().decode(response, JSONObject.class))); } + @Test + public void decodesString() throws IOException { + String json = "qwerty"; + Response response = Response.builder() + .status(200) + .reason("OK") + .headers(Collections.emptyMap()) + .body(json, UTF_8) + .request(request) + .build(); + assertEquals("qwerty", new JsonDecoder().decode(response, String.class)); + } + @Test public void notFoundDecodesToEmpty() throws IOException { Response response = Response.builder() @@ -100,6 +113,17 @@ public void nullBodyDecodesToEmpty() throws IOException { assertTrue(((JSONObject) new JsonDecoder().decode(response, JSONObject.class)).isEmpty()); } + @Test + public void nullBodyDecodesToNullString() throws IOException { + Response response = Response.builder() + .status(204) + .reason("OK") + .headers(Collections.emptyMap()) + .request(request) + .build(); + assertNull(new JsonDecoder().decode(response, String.class)); + } + @Test public void emptyBodyDecodesToEmpty() throws IOException { Response response = Response.builder()