FasterXML / FasterXML/jackson-modules-base

NoCtorDeser can not deserialize parameterized types

オープン
#193 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る
no-ctor-deser
主要言語
Java
スター
180
フォーク
80
平均マージ
3時間 26分
マージ済み PR(30日)
1

説明

# Code
The following code throws java.lang.ClassCastException. The type of `response.getPayload()` is not `User` but `LinkedHashMap`

```java
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.module.noctordeser.NoCtorDeserModule;

public class Demo {

public static class Response {

private final T payload;

public Response(T payload) {
this.payload = payload;
}

public T getPayload() {
return payload;
}
}

public static class User {

private final String name;

public User(String name) {
this.name = name;
}

public String getName() {
return name;
}
}

public static void main(String[] args) throws Exception {
String json = "{\"payload\":{\"name\":\"jackson\"}}";
JsonMapper mapper = JsonMapper.builder()
.addModule(new NoCtorDeserModule())
.build();

ObjectReader objectReader = mapper.readerFor(new TypeReference>() {
});
Response response = objectReader.readValue(json);
System.out.println(response.getPayload().getClass());
}
}
```

# Cause
@cowtowncoder It seems the JsonDeserializer is fetched by `Class instClass` rather than a ParameterizedType.

https://github.com/FasterXML/jackson-modules-base/blob/2.15/no-ctor-deser/src/main/java/com/fasterxml/jackson/module/noctordeser/MissingInstantiatorHandler.java#L34-L37

# Solution
The overrided method `DeserializationProblemHandler#handleMissingInstantiator(DeserializationContext ctxt, Class instClass, ValueInstantiator valueInst, JsonParser jsonParser, String msg)` does not suppy generic information. Maybe we can extend class AnnotatedWithParams and override its `public abstract Object call() throws Exception;`. Then we set it to the StdValueInstantiator's _defaultCreator field?

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。