Browse Source

JSON Decoder can decode String too (#1989)

pull/1990/head
Witalij Berdinskich 2 years ago committed by GitHub
parent
commit
8ad08f80d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      json/src/main/java/feign/json/JsonDecoder.java
  2. 24
      json/src/test/java/feign/json/JsonDecoderTest.java

9
json/src/main/java/feign/json/JsonDecoder.java

@ -14,6 +14,7 @@ @@ -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 class JsonDecoder implements Decoder { @@ -57,6 +58,8 @@ public class JsonDecoder implements Decoder {
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 @@ public class JsonDecoder implements Decoder { @@ -69,7 +72,7 @@ public class JsonDecoder implements Decoder {
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 @@ public class JsonDecoder implements Decoder { @@ -79,7 +82,9 @@ public class JsonDecoder implements Decoder {
}
}
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);

24
json/src/test/java/feign/json/JsonDecoderTest.java

@ -78,6 +78,19 @@ public class JsonDecoderTest { @@ -78,6 +78,19 @@ public class JsonDecoderTest {
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 class JsonDecoderTest { @@ -100,6 +113,17 @@ public class JsonDecoderTest {
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()

Loading…
Cancel
Save