Currently deserializing with GSON and retrofit using retrofits GsonConverterFactory:
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(new TypeToken<Map<Book, Collection<Author>>>(){}.getType(), new BooksDeserializer(context));
Gson gson = gsonBuilder.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
BookService service = retrofit.create(BookService.class);
Response<Map<Book, Collection<Author>>> response = service.getBooks().execute();
I would like to use the JacksonConverterFactory that is provided by retrofit? I would need to provide that a Jackson mapper. Is there a way to provide the type information to that mapper like I did with GSON?
SimpleModule simpleModule = new SimpleModule();
// TODO provide mapper with info needed to deserialize
// Map<Book, Collection<Author>>
mapper.registerModule(simpleModule);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(JacksonConverterFactory.create(mapper))
.build();
BookService service = retrofit.create(BookService.class);
Response<Map<Book, Collection<Author>>> response = service.getBooks().execute();
Looking specifically at the TODO can I tell the mapper to use this deserializer?
public class BooksDeserializer extends JsonDeserializer<Map<Book, Collection<Author>>> {
@Override
public Map<Book, Collection<Author>> deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException {
// deserialize here
}
}