Moja struktura JSON:JsonSubTypes, lista polymorpic obiektów i Parcelable
{
...
"type":"post", // Type could vary
"items":[] // Array of items, each item is typeOf("type")
...
}
Jak mogę deserializowania i prawidłowo Parcel items
listy wewnątrz mojego POJO:
public class ItemsEnvelope {
private String type;
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXTERNAL_PROPERTY,
property = "type",
visible = true)
@JsonSubTypes({
@JsonSubTypes.Type(value = A.class, name = "A"),
@JsonSubTypes.Type(value = B.class, name = "B"),
@JsonSubTypes.Type(value = C.class, name = "C")
})
private List<Item> items;
interface Item extends Parcelable {}
class A implements Item {
// Bunch of getters/setters and Parcelable methods/constructor
}
class B implements Item {
// Bunch of getters/setters and Parcelable methods/constructor
}
class C implements Item {
// Bunch of getters/setters and Parcelable methods/constructor
}
// Bunch of getters/setters and Parcelable methods/constructor
}
Aby Parcel wpisywanych z wykazu CREATOR
obiekt powinien być zapewnione, czego interfejs oczywiście nie może mieć. Czy powinienem używać klasy abstrakcyjnej zamiast interfejsu?
Dodano strukturę json do pytania. Jaką metodę integracji powinienem zastosować w moim przypadku? – localhost
Nie ma możliwości wskazania typów wszystkich elementów tablicy JSON; Jackson oczekuje informacji o typie każdego elementu. Aby wesprzeć tę aranżację, musisz napisać niestandardowy deserializator (i serializator, jeśli chcesz produkować taki JSON). – StaxMan