Skip to content

Commit a6b45dc

Browse files
committed
Use type resolver in JSONDeserializer to find the proper implementation class for a specific API interface.
1 parent d5ecb19 commit a6b45dc

File tree

1 file changed

+37
-14
lines changed

1 file changed

+37
-14
lines changed

src/main/java/com/github/m0nk3y2k4/thetvdb/internal/util/JsonDeserializer.java

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import com.fasterxml.jackson.core.type.TypeReference;
1515
import com.fasterxml.jackson.databind.JsonNode;
1616
import com.fasterxml.jackson.databind.ObjectMapper;
17+
import com.fasterxml.jackson.databind.module.SimpleAbstractTypeResolver;
18+
import com.fasterxml.jackson.databind.module.SimpleModule;
1719
import com.github.m0nk3y2k4.thetvdb.api.exception.APIException;
1820
import com.github.m0nk3y2k4.thetvdb.api.model.data.Actor;
1921
import com.github.m0nk3y2k4.thetvdb.api.model.data.Episode;
@@ -40,6 +42,27 @@
4042

4143
public final class JsonDeserializer {
4244

45+
/** Object mapper module used to extend the mappers functionality in terms of properly mapping the APIs interfaces */
46+
private static final SimpleModule MODULE = new SimpleModule();
47+
48+
static {
49+
// Add Interface <-> Implementation mappings to the module. The object mapper will use these mappings to
50+
// determine and instantiate the proper implementation class for a specific interface.
51+
MODULE.setAbstractTypes(new SimpleAbstractTypeResolver()
52+
.addMapping(SeriesSearchResult.class, SeriesSearchResultImpl.class)
53+
.addMapping(Series.class, SeriesImpl.class)
54+
.addMapping(Episode.class, EpisodeImpl.class)
55+
.addMapping(Language.class, LanguageImpl.class)
56+
.addMapping(Actor.class, ActorImpl.class)
57+
.addMapping(SeriesSummary.class, SeriesSummaryImpl.class)
58+
.addMapping(ImageQueryParameter.class, ImageQueryParameterImpl.class)
59+
.addMapping(ImageSummary.class, ImageSummaryImpl.class)
60+
.addMapping(Image.class, ImageImpl.class)
61+
.addMapping(Rating.class, RatingImpl.class)
62+
.addMapping(User.class, UserImpl.class)
63+
);
64+
}
65+
4366
private JsonDeserializer() {} // Private constructor. Only static methods
4467

4568
public static List<String> mapQueryParameters(@Nonnull JsonNode json) throws APIException {
@@ -57,59 +80,59 @@ public static Map<String, String> mapSeriesHeader(@Nonnull JsonNode json) throws
5780
}
5881

5982
public static List<SeriesSearchResult> mapSeriesSearchResult(@Nonnull JsonNode json) throws APIException {
60-
return mapObjects(getData(json), SeriesSearchResultImpl.class);
83+
return mapObjects(getData(json), SeriesSearchResult.class);
6184
}
6285

6386
public static Series mapSeries(@Nonnull JsonNode json) throws APIException {
64-
return mapObject(getData(json), SeriesImpl.class);
87+
return mapObject(getData(json), Series.class);
6588
}
6689

6790
public static Episode mapEpisode(@Nonnull JsonNode json) throws APIException {
68-
return mapObject(getData(json), EpisodeImpl.class);
91+
return mapObject(getData(json), Episode.class);
6992
}
7093

7194
public static List<Episode> mapEpisodes(@Nonnull JsonNode json) throws APIException {
72-
return mapObjects(getData(json), EpisodeImpl.class);
95+
return mapObjects(getData(json), Episode.class);
7396
}
7497

7598
public static List<Language> mapLanguages(@Nonnull JsonNode json) throws APIException {
76-
return mapObjects(getData(json), LanguageImpl.class);
99+
return mapObjects(getData(json), Language.class);
77100
}
78101

79102
public static Language mapLanguage(@Nonnull JsonNode json) throws APIException {
80-
return mapObject(getData(json), LanguageImpl.class);
103+
return mapObject(getData(json), Language.class);
81104
}
82105

83106
public static List<Actor> mapActors(@Nonnull JsonNode json) throws APIException {
84-
return mapObjects(getData(json), ActorImpl.class);
107+
return mapObjects(getData(json), Actor.class);
85108
}
86109

87110
public static SeriesSummary mapSeriesSummary(@Nonnull JsonNode json) throws APIException {
88-
return mapObject(getData(json), SeriesSummaryImpl.class);
111+
return mapObject(getData(json), SeriesSummary.class);
89112
}
90113

91114
public static List<ImageQueryParameter> mapImageQueryParameters(@Nonnull JsonNode json) throws APIException {
92-
return mapObjects(getData(json), ImageQueryParameterImpl.class);
115+
return mapObjects(getData(json), ImageQueryParameter.class);
93116
}
94117

95118
public static ImageSummary mapSeriesImageSummary(@Nonnull JsonNode json) throws APIException {
96-
return mapObject(getData(json), ImageSummaryImpl.class);
119+
return mapObject(getData(json), ImageSummary.class);
97120
}
98121

99122
public static List<Image> mapImages(@Nonnull JsonNode json) throws APIException {
100-
return mapObjects(getData(json), ImageImpl.class);
123+
return mapObjects(getData(json), Image.class);
101124
}
102125

103126
public static Map<Long, Long> mapUpdates(@Nonnull JsonNode json) {
104127
return StreamSupport.stream(getData(json).spliterator(), false).collect(Collectors.toMap(x -> x.get("id").asLong(), x -> x.get("lastUpdated").asLong()));
105128
}
106129

107130
public static List<Rating> mapRatings(@Nonnull JsonNode json) throws APIException {
108-
return mapObjects(getData(json), RatingImpl.class);
131+
return mapObjects(getData(json), Rating.class);
109132
}
110133

111134
public static User mapUser(@Nonnull JsonNode json) throws APIException {
112-
return mapObject(getData(json), UserImpl.class);
135+
return mapObject(getData(json), User.class);
113136
}
114137

115138
private static JsonNode getData(@Nonnull JsonNode json) {
@@ -128,7 +151,7 @@ private static <I, T extends I> List<I> mapObjects(@Nonnull JsonNode json, @Nonn
128151

129152
private static <T> T mapObject(@Nonnull JsonNode json, @Nonnull Class<T> clazz) throws APIException {
130153
try {
131-
return new ObjectMapper().readValue(json.toString(), clazz);
154+
return new ObjectMapper().registerModule(MODULE).readValue(json.toString(), clazz);
132155
} catch (IOException ex) {
133156
throw new APIException(API_JSON_PARSE_ERROR, ex);
134157
}

0 commit comments

Comments
 (0)