-
Lets say I have the following code: public class GenericsDeserializationDemo {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
Wrapper<WrappableImpl> value = mapper.readValue("{\"wrappedList\":[{\"id\":123}]}", new TypeReference<Wrapper<WrappableImpl>>() {
});
System.out.println(value.getWrappedList().get(0).getId());
}
}
public interface Wrappable {
int getId();
}
public class WrappableImpl implements Wrappable {
private int id;
@Override
public int getId() {
return id;
}
}
public class WrappedListDeserializer extends StdDeserializer<List<? extends Wrappable>> {
protected WrappedListDeserializer() {
super((Class<?>) null);
}
@Override
public List<? extends Wrappable> deserialize(JsonParser p, DeserializationContext ctxt) {
JavaType type = getValueType(); // returns null
// How do I get the actual type?
return null;
}
}
public class Wrapper<T extends Wrappable> {
@JsonDeserialize(using = WrappedListDeserializer.class)
private List<T> wrappedList;
public List<T> getWrappedList() {
return wrappedList;
}
} Is there any chance to get |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Try invoking |
Beta Was this translation helpful? Give feedback.
-
After some research I found a solution that works for me. Here's my updated public class WrappedListDeserializer<T extends Wrappable> extends JsonDeserializer<List<T>> implements ContextualDeserializer {
private JavaType type;
@Override
public List<T> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return ctxt.readValue(p, type);
}
@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property) {
WrappedListDeserializer<T> deserializer = new WrappedListDeserializer<>();
deserializer.type = property.getType();
return deserializer;
}
} All other classes remain the same. |
Beta Was this translation helpful? Give feedback.
-
That is one way for sure. But in general, full generic type should be available to |
Beta Was this translation helpful? Give feedback.
After some research I found a solution that works for me. Here's my updated
WrappedListDeserializer
:A…