FasterXML / FasterXML/jackson-modules-base

NoCtorDeser can not deserialize parameterized types

Open
#193 1 comment 0 reactions 0 assignees View on GitHub
no-ctor-deser
Dominant language
Java
Stars
180
Forks
80
Avg merge
3h 26m
Merged PRs (30d)
1

Description

# 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?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.