Skip to content

Commit 55df580

Browse files
committed
feat: add de-serialization logic for JSON arrays within response body
1 parent 72029dd commit 55df580

File tree

3 files changed

+250
-24
lines changed

3 files changed

+250
-24
lines changed

src/main/java/com/ibm/cloud/sdk/core/util/ResponseConverterUtils.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ public InputStream convert(Response response) {
6565
}
6666

6767
/**
68-
* Creates a generic {@link ResponseConverter} for a POJO class. <br>
69-
* It should extend {@link ObjectModel}
68+
* Creates a generic {@link ResponseConverter} for a POJO class that extends ObjectModel.
7069
*
7170
* @param <T> the generic type
7271
* @param type the type
@@ -81,6 +80,17 @@ public T convert(Response response) {
8180
};
8281
}
8382

83+
/**
84+
* Creates a generic {@link ResponseConverter} for a POJO class.
85+
*
86+
* @param <T> the generic type
87+
* @param type a Type instance that describes the type
88+
* @return the response converter
89+
*/
90+
public static <T> ResponseConverter<T> getObject(final Type type) {
91+
return getValue(type);
92+
}
93+
8494
/**
8595
* Creates a generic {@link ResponseConverter} for a String response.
8696
*
@@ -111,6 +121,23 @@ public T convert(Response response) {
111121
};
112122
}
113123

124+
/**
125+
* Creates a generic {@link ResponseConverter} for a response with a Type
126+
* instance that describes the type.
127+
* @param <T> the generic type
128+
* @param type the type
129+
*
130+
* @return the response converter
131+
*/
132+
public static <T> ResponseConverter<T> getValue(final Type type) {
133+
return new ResponseConverter<T>() {
134+
@Override
135+
public T convert(Response response) {
136+
return ResponseUtils.getValue(response, type);
137+
}
138+
};
139+
}
140+
114141
/**
115142
* Gets the void converter.
116143
* @return the void converter

src/main/java/com/ibm/cloud/sdk/core/util/ResponseUtils.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import java.io.IOException;
1616
import java.io.InputStream;
17+
import java.lang.reflect.Type;
1718
import java.util.logging.Level;
1819
import java.util.logging.Logger;
1920

@@ -96,6 +97,24 @@ public static <T extends ObjectModel> T getObject(Response response, Class<? ext
9697
}
9798
}
9899

100+
101+
/**
102+
* Parses the {@link Response} into the POJO representation.
103+
*
104+
* @param <T> the generic type to use when parsing the response
105+
* @param response the HTTP response
106+
* @param type a Type instance which describes the type of the response
107+
* @return the POJO
108+
*/
109+
public static <T> T getObject(Response response, Type type) {
110+
JsonReader reader;
111+
try {
112+
reader = new JsonReader(response.body().charStream());
113+
return GsonSingleton.getGsonWithoutPrettyPrinting().fromJson(reader, type);
114+
} finally {
115+
response.body().close();
116+
}
117+
}
99118
/**
100119
* Parses the {@link Response} into a value of the specified type.
101120
*
@@ -114,6 +133,24 @@ public static <T> T getValue(Response response, Class<? extends T> valueType) {
114133
}
115134
}
116135

136+
/**
137+
* Parses the {@link Response} into a value of the specified type.
138+
*
139+
* @param <T> the generic type to use when parsing the response
140+
* @param response the HTTP response
141+
* @param valueType a Type instance which describes the type associated with the response
142+
* @return the value
143+
*/
144+
public static <T> T getValue(Response response, Type valueType) {
145+
JsonReader reader;
146+
try {
147+
reader = new JsonReader(response.body().charStream());
148+
return GsonSingleton.getGsonWithoutPrettyPrinting().fromJson(reader, valueType);
149+
} finally {
150+
response.body().close();
151+
}
152+
}
153+
117154
/**
118155
* Returns a String representation of the response.
119156
*

0 commit comments

Comments
 (0)