我有下面的示例類:傑克遜反序列多態類型
class Zoo {
public Collection<? extends Animal> animals;
}
當序列化,我想通過做連載不亞於類型的信息,我可以:
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
而我得到以下JSON:
[
"com.bp.samples.json.generics.Zoo",
{
"animals": [
"java.util.ArrayList",
[
[
"com.bp.samples.json.generics.Bird",
{
"name": "bird-1",
"wingSpan": "6 feets",
"preferredFood": "food-1"
}
],
[
"com.bp.samples.json.generics.Cat",
{
"name": "cat-1",
"favoriteToy": "toy-1"
}
],
[
"com.bp.samples.json.generics.Dog",
{
"name": "dog-1",
"breed": "bread-1",
"leashColor": "black"
}
]
]
]
}
]
當談到反序列化,努力做到:
mapper.readValue(new File("./DataFiles/Zoo-2.json"), Zoo.class);
導致以下異常:
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException:
Can not deserialize instance of com.bp.samples.json.generics.Zoo out of START_ARRAY toke
,努力做到:
mapper.readValue(new File("./DataFiles/Zoo-2.json"),
new TypeReference<Collection<? extends Animal>>() {});
結果:
Can not construct instance of com.bp.samples.json.generics.Animal,
problem: abstract types either need to be mapped to concrete types, have custom
deserializer, or be instantiated with additional type information
at [Source: ./DataFiles/Zoo-2.json; line: 2, column: 5]
編寫自定義解串器,斷當然,解決了這個問題,但是在那裏去了ialize沒有自定義反序列化器?
謝謝,你啓用類型的信息爲你使用反序列化ObjectMapper實例 Behzad
將JSON數組(集合)反序列化爲Java對象(Zoo - 不是集合)將不起作用(沒有特殊處理)。你必須使用DefaultTyping來提供類型信息嗎?您可能想要查看完整的多態反序列化示例http://programmerbruce.blogspot.com/2011/05/deserialize-json-with-jackson-into.html –
我建議您選擇一個標記來替換java(以獲取這個問題更多關注)。 –
@Programmer_Bruce我想這裏的數組是由於默認的類型信息包含,所以應該沒問題。 – StaxMan