Как работает полиморфизм с Gson (Retrofit)
Вот мой экземпляр Retrofit
:
@Provides
@Singleton
ApiManager provideApiManager() {
RxJava2CallAdapterFactory rxAdapter = RxJava2CallAdapterFactory.create();
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addNetworkInterceptor(new StethoInterceptor())
.build();
Gson gson = new GsonBuilder().create();
GsonConverterFactory converterFactory = GsonConverterFactory.create(gson);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(AppConstants.BASE_URL)
.addConverterFactory(converterFactory)
.addCallAdapterFactory(rxAdapter)
.client(okHttpClient)
.build();
return retrofit.create(ApiManager.class);
}
Модель:
class AbstractMessage {
String id;
}
class TextMessage extends AbstractMessage {
String textMessage;
}
class ImageMessage extends AbstractMessage {
String url;
String text;
}
Запрос:
@GET("direct/messages")
Observable<List<AbstractMessage>> getMessages(@Header("Authorization") String authHeader, @Body RequestObject request);
Выполнение запроса:
apiManager.getMessages(authHeader, requestObject)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<List<AbstractMessage>>() {
@Override
public void accept(List<AbstractMessage> messages) throws Exception {
...
}
});
Когда я выполняю запрос, я получаю коллекцию объектов AbstractMessage
. JSON
может содержать как текстовые, так и графические сообщения. В моем случае преобразователь JSON
создает AbstractMessage
и отображает только поле id
. Как я могу сделать конвертер для создания объектов TextMessage
и ImageMessage
, сопоставляющих все соответствующие поля, а затем отбрасывать их на AbstractMessage
. Или может быть какое-то другое решение.
Ответы
Ответ 1
Вы должны создать RuntimeTypeAdapterFactory для объектов AbstractMessage, TextMessage и ImageMessage, а затем вы должны установить его в экземпляр gson.
Предположим, что у вас есть эти объекты:
public class Animal {
protected String name;
protected String type;
public Animal(String name, String type) {
this.name = name;
this.type = type;
}
}
public class Dog extends Animal {
private boolean playsCatch;
public Dog(String name, boolean playsCatch) {
super(name, "dog");
this.playsCatch = playsCatch;
}
}
public class Cat extends Animal {
private boolean chasesLaser;
public Cat(String name, boolean chasesLaser) {
super(name, "cat");
this.chasesLaser = chasesLaser;
}
}
Ниже приведена RuntimeTypeAdapter, которая вам нужна для десериализации (и сериализации) этих объектов:
RuntimeTypeAdapterFactory<Animal> runtimeTypeAdapterFactory = RuntimeTypeAdapterFactory
.of(Animal.class, "type")
.registerSubtype(Dog.class, "dog")
.registerSubtype(Cat.class, "cat");
Gson gson = new GsonBuilder()
.registerTypeAdapterFactory(runtimeTypeAdapterFactory)
.create();
Класс RuntimeTypeAdapterFactory.java не поставляется с пакетом Gson, поэтому вам нужно загрузить и добавить его вручную в свой проект.
Вы можете узнать больше об адаптере времени выполнения здесь и здесь
Обратите внимание, что название вашего вопроса должно быть "Полиморфизм с помощью Gson"
Надеюсь, это поможет.