|
3 | 3 | import static com.github.m0nk3y2k4.thetvdb.api.exception.APIException.API_JSON_PARSE_ERROR; |
4 | 4 |
|
5 | 5 | import java.io.IOException; |
| 6 | +import java.util.Collections; |
6 | 7 | import java.util.List; |
7 | 8 | import java.util.Map; |
8 | 9 | import java.util.function.Function; |
| 10 | +import java.util.function.Supplier; |
9 | 11 | import java.util.stream.Collectors; |
10 | 12 | import java.util.stream.StreamSupport; |
11 | 13 |
|
@@ -86,10 +88,15 @@ public static APIResponse<List<String>> mapQueryParameters(@Nonnull JsonNode jso |
86 | 88 | } |
87 | 89 |
|
88 | 90 | public static APIResponse<List<String>> mapFavorites(@Nonnull JsonNode json) throws APIException { |
89 | | - Function<JsonNode, List<String>> dataFunction = |
90 | | - node -> StreamSupport.stream(getData(node).get("favorites").spliterator(), false) |
91 | | - .map(JsonNode::asText).collect(Collectors.toList()); |
92 | | - return mapObject(json, new TypeReference<>(){}, createFunctionalModule(dataFunction)); |
| 91 | + if (getData(json).has("favorites")) { |
| 92 | + // If the user has no favorites just an empty data-node might be returned |
| 93 | + Function<JsonNode, List<String>> dataFunction = |
| 94 | + node -> StreamSupport.stream(getData(node).get("favorites").spliterator(), false) |
| 95 | + .map(JsonNode::asText).collect(Collectors.toList()); |
| 96 | + return mapObject(json, new TypeReference<>() {}, createFunctionalModule(dataFunction)); |
| 97 | + } |
| 98 | + |
| 99 | + return mapObject(json, new TypeReference<>(){}, createFunctionalModule(Collections::emptyList)); |
93 | 100 | } |
94 | 101 |
|
95 | 102 | public static Map<String, String> mapSeriesHeader(@Nonnull JsonNode json) throws APIException { |
@@ -163,6 +170,11 @@ private static <T> Module createFunctionalModule(@Nonnull Function<JsonNode, T> |
163 | 170 | return new SimpleModule().addDeserializer(APIResponse.class, new FunctionalDeserializer<>(dataFunction)); |
164 | 171 | } |
165 | 172 |
|
| 173 | + private static <T> Module createFunctionalModule(@Nonnull Supplier<T> supplier) { |
| 174 | + Function<JsonNode, T> dataFunction = node -> supplier.get(); |
| 175 | + return new SimpleModule().addDeserializer(APIResponse.class, new FunctionalDeserializer<>(dataFunction)); |
| 176 | + } |
| 177 | + |
166 | 178 | private static <T> T mapObject(@Nonnull JsonNode json, @Nonnull TypeReference<T> typeReference) throws APIException { |
167 | 179 | return mapObject(json, typeReference, DFAULT_MODULE); |
168 | 180 | } |
|
0 commit comments