forked from OpenFeign/feign
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonDecoder.java
More file actions
93 lines (88 loc) · 3.35 KB
/
JsonDecoder.java
File metadata and controls
93 lines (88 loc) · 3.35 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* Copyright 2012-2023 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package feign.json;
import feign.Response;
import feign.codec.DecodeException;
import feign.codec.Decoder;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.Type;
import static java.lang.String.format;
/**
* Decodes responses using JSON-java.
* <p>
* Basic example with {@link feign.Feign.Builder}:
*
* <pre>
* interface GitHub {
*
* {@literal @}RequestLine("GET /repos/{owner}/{repo}/contributors")
* JSONArray contributors({@literal @}Param("owner") String owner, {@literal @}Param("repo") String repo);
*
* }
*
* GitHub github = Feign.builder()
* .decoder(new JsonDecoder())
* .target(GitHub.class, "https://api.github.com");
*
* JSONArray contributors = github.contributors("openfeign", "feign");
*
* System.out.println(contributors.getJSONObject(0).getString("login"));
* </pre>
*/
public class JsonDecoder implements Decoder {
@Override
public Object decode(Response response, Type type) throws IOException, DecodeException {
if (response.status() == 404 || response.status() == 204)
if (JSONObject.class.isAssignableFrom((Class<?>) type))
return new JSONObject();
else if (JSONArray.class.isAssignableFrom((Class<?>) type))
return new JSONArray();
else
throw new DecodeException(response.status(),
format("%s is not a type supported by this decoder.", type), response.request());
if (response.body() == null)
return null;
try (Reader reader = response.body().asReader(response.charset())) {
Reader bodyReader = (reader.markSupported()) ? reader : new BufferedReader(reader);
bodyReader.mark(1);
if (bodyReader.read() == -1) {
return null; // Empty body
}
bodyReader.reset();
return decodeJSON(response, type, bodyReader);
} catch (JSONException jsonException) {
if (jsonException.getCause() != null && jsonException.getCause() instanceof IOException) {
throw (IOException) jsonException.getCause();
}
throw new DecodeException(response.status(), jsonException.getMessage(), response.request(),
jsonException);
}
}
private Object decodeJSON(Response response, Type type, Reader reader) {
JSONTokener tokenizer = new JSONTokener(reader);
if (JSONObject.class.isAssignableFrom((Class<?>) type))
return new JSONObject(tokenizer);
else if (JSONArray.class.isAssignableFrom((Class<?>) type))
return new JSONArray(tokenizer);
else
throw new DecodeException(response.status(),
format("%s is not a type supported by this decoder.", type), response.request());
}
}