3 回答

TA貢獻(xiàn)1898條經(jīng)驗(yàn) 獲得超8個(gè)贊
為了擴(kuò)展他的示例,可以通過以下更改使他的適配器類通用以適用于所有類型的對(duì)象(不僅僅是IAnimal):
class InheritanceAdapter<T> implements JsonSerializer<T>, JsonDeserializer<T>
{
....
? ? public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context)
....
? ? public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
....
}
在測(cè)試類中:
public class Test {
? ? public static void main(String[] args) {
? ? ? ? ....
? ? ? ? ? ? builder.registerTypeAdapter(IAnimal.class, new InheritanceAdapter<IAnimal>());
? ? ? ? ....
}
添加回答
舉報(bào)