MCPcopy Index your code
hub / github.com/OpenFeign/feign / MoshiDecoder

Class MoshiDecoder

moshi/src/main/java/feign/moshi/MoshiDecoder.java:30–70  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

28import okio.Okio;
29
30public class MoshiDecoder implements Decoder, JsonDecoder {
31 private final Moshi moshi;
32
33 public MoshiDecoder(Moshi moshi) {
34 this.moshi = moshi;
35 }
36
37 public MoshiDecoder() {
38 this.moshi = new Moshi.Builder().build();
39 }
40
41 public MoshiDecoder(Iterable<JsonAdapter<?>> adapters) {
42 this(MoshiFactory.create(adapters));
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 {
67 JsonAdapter<Object> adapter = moshi.adapter(type);
68 return adapter.fromJsonValue(object);
69 }
70}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected