Skip to content

Commit f0d6f69

Browse files
meili-bors[bot]fanpeng
and
fanpeng
authored
Merge #738
738: Adjust the targetClass parameter variable of the decode method in JsonHandler from class<?> to class<T> r=curquiza a=Mashull ## Related issue Refactor #737 ## What does this PR do? The targetClass parameter should be of type Class<T>, not Class<?>. This provides stronger type safety because you can check the type of T at compile time rather than at run time Co-authored-by: fanpeng <[email protected]>
2 parents a3b336a + de26842 commit f0d6f69

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

src/main/java/com/meilisearch/sdk/json/GsonJsonHandler.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public String encode(Object o) throws MeilisearchException {
3434

3535
@Override
3636
@SuppressWarnings("unchecked")
37-
public <T> T decode(Object o, Class<?> targetClass, Class<?>... parameters)
37+
public <T> T decode(Object o, Class<T> targetClass, Class<?>... parameters)
3838
throws MeilisearchException {
3939
if (o == null) {
4040
throw new JsonDecodingException("Response to deserialize is null");
@@ -44,10 +44,10 @@ public <T> T decode(Object o, Class<?> targetClass, Class<?>... parameters)
4444
}
4545
try {
4646
if (parameters == null || parameters.length == 0) {
47-
return gson.<T>fromJson((String) o, targetClass);
47+
return gson.fromJson((String) o, targetClass);
4848
} else {
4949
TypeToken<?> parameterized = TypeToken.getParameterized(targetClass, parameters);
50-
return gson.<T>fromJson((String) o, parameterized.getType());
50+
return gson.fromJson((String) o, parameterized.getType());
5151
}
5252
} catch (JsonSyntaxException e) {
5353
throw new JsonDecodingException(e);

src/main/java/com/meilisearch/sdk/json/JacksonJsonHandler.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public String encode(Object o) throws MeilisearchException {
4848
/** {@inheritDoc} */
4949
@SuppressWarnings("unchecked")
5050
@Override
51-
public <T> T decode(Object o, Class<?> targetClass, Class<?>... parameters)
51+
public <T> T decode(Object o, Class<T> targetClass, Class<?>... parameters)
5252
throws MeilisearchException {
5353
if (o == null) {
5454
throw new JsonDecodingException("Response to deserialize is null");
@@ -58,7 +58,7 @@ public <T> T decode(Object o, Class<?> targetClass, Class<?>... parameters)
5858
}
5959
try {
6060
if (parameters == null || parameters.length == 0) {
61-
return (T) mapper.readValue((String) o, targetClass);
61+
return mapper.readValue((String) o, targetClass);
6262
} else {
6363
return mapper.readValue(
6464
(String) o,

src/main/java/com/meilisearch/sdk/json/JsonHandler.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ public interface JsonHandler {
1919
* @return the deserialized object
2020
* @throws MeilisearchException wrapped exceptions of the used json library
2121
*/
22-
<T> T decode(Object o, Class<?> targetClass, Class<?>... parameters)
22+
<T> T decode(Object o, Class<T> targetClass, Class<?>... parameters)
2323
throws MeilisearchException;
2424
}

0 commit comments

Comments
 (0)