enableDefaultTyping とジェネリック TypeRefernce の両方を使用すると、逆シリアル化の問題が発生しました。Jackson は、どちらの型情報がより重要であるかを判断できないようです。このテスト ケースは、問題を示しています。
@Test
public void roundTripTest() throws JsonGenerationException,
JsonMappingException, IOException {
// 0 Value Test
Integer[] integers = new Integer[] {};
Wrap<Integer[]> beforeResult = new Wrap<Integer[]>(integers);
File file = new File("/tmp/jsonTest");
mapper.writeValue(file, beforeResult);
TypeReference<Wrap<Integer[]>> typeRef = new TypeReference<JacksonMapperTest.Wrap<Integer[]>>() {
};
Wrap<Integer[]> afterResult = mapper.readValue(file, typeRef);
assertNotNull(afterResult);
}
public static class Wrap<T> {
private T wrapped;
public Wrap() {
}
public Wrap(T wrapped) {
this.wrapped = wrapped;
}
public T getWrapped() {
return wrapped;
}
public void setWrapped(T wrapped) {
this.wrapped = wrapped;
}
}
マッパーは次のとおりです。
mapper = new ObjectMapper();
mapper.enableDefaultTyping();
例外は次のとおりです。
org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class JacksonMapperTest$Wrap<[Ljava.lang.Integer;>]: can not instantiate from JSON object (need to add/enable type information?)
かなり奇妙ですね。TypeRefernce の代わりに beforeResult.getClass を使用することで問題を回避できますが、それでも好ましい動作ではありません。
これを解決するために、何かオプションを見逃していませんか?
Jackson 1.9.3を使用していました
[編集]ラップされたオブジェクトとして配列の代わりにマップを使用すると、期待どおりに機能します!