Closed
Description
I'm trying to use content converter with Guava collections like so:
@JsonDeserialize(contentConverter = DoublingConverter.class)
public ImmutableList<Integer> ints;
However this does not seem to do anything despite Jackson having deserializers for Guava.
Changing collection to one of built-in types makes the program work.
I would expect that if Jackson can deserialize given collection type, it will also be able to apply converters to it.
Posting here rather than to datatypes project since this is not related to specific collection implementation but rather a generic problem.
TestImmutableListContentConverter .java
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.util.StdConverter;
import com.fasterxml.jackson.datatype.guava.GuavaModule;
import com.google.common.collect.ImmutableList;
public class TestImmutableListContentConverter {
static class Holder {
@JsonDeserialize(contentConverter = DoublingConverter.class)
public ImmutableList<Integer> ints;
}
static class DoublingConverter extends StdConverter<Integer, Integer> {
@Override
public Integer convert(Integer value) {
return value != null ? value * 2 : null;
}
}
public static void main(String[] args) throws Exception {
var json = """
{ "ints": [1,2,3] }
""";
var om = new ObjectMapper();
om.registerModule(new GuavaModule());
var value = om.readValue(json, Holder.class);
System.out.println(value.ints);
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>test-jackson-guava</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-guava</artifactId>
<version>2.13.0-rc1</version>
</dependency>
</dependencies>
</project>